[英]Why am I getting a null reference exception when rendering this razor view?
我按照有關udemy的MVC教程進行操作,但出現空引用錯誤。 當我嘗試獲取Model.Name時
我不確定我在這里做錯了什么。我的代碼與教程中解釋的完全相同。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;
namespace Vidly.Controllers
{
public class MoviesController : Controller
{
// GET: Movies/Random
public ActionResult Random()
{
var movie = new Movie() { Name="Shrek" };
return View();
}
}
}
我的觀點是:
@model Vidly.Models.Movie
@{
ViewBag.Title = "Random";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@Model.Id</h2>
您需要將模型傳遞回視圖:
return View(movie);
您尚未將模型傳遞給視圖。
public class MoviesController : Controller
{
// GET: Movies/Random
public ActionResult Random()
{
var movie = new Movie() { Name="Shrek" };
//important line
return View(movie);
}
}
您忘記了將模型傳遞給視圖。
return View(movie);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.