0

I'm experiencing Asp.net MVC5. and I did the following.

Model Name is Movie, with the code as below,

    namespace mvc1.Models
{
    public class Movie
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

And a Controller with name 'MoviesConroller'. and a using statement "using mvc1.Models".

public ActionResult Random()
{

    var movie = new Movie() { Name = "Star Trek!" };
    return View();
}

And a View with name Random

@model mvc1.Models.Movie
    @{
    ViewBag.Title = "Random";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Name</h2>

and this is the error I get this Error

1 Answers1

0

You need to pass object Model to be View function parameter, otherwise razor view @Model will be NULL

use protected internal ViewResult View(object model) this overload function.

public ActionResult Random()
{

    var movie = new Movie() { Name = "Star Trek!" };
    return View(movie);
}
D-Shih
  • 42,799
  • 6
  • 22
  • 44
  • thanks for the answer, it did works. but what is this "protected internal ViewResult View(object model)" – Mostafa Nafady Jul 08 '18 at 19:05
  • You can see asp.net MVC source-code if you use `View()` `ViewData.Model` will be null ,`@Model` is `ViewData.Model` – D-Shih Jul 08 '18 at 19:10
  • this GitHub repository can debug from ASP.NET MVC source code https://github.com/isdaniel/Asp.net-MVC-Debuger/blob/master/src/aspnetwebstack/src/System.Web.Mvc/Controller.cs – D-Shih Jul 08 '18 at 19:13