2017-02-08 49 views
0

我搜索無法找到答案。'System.NullReferenceException'發生在App_Web_zqz5irkc.dll中,但未在用戶代碼中處理

不能找到哪裏是空以及如何解決它

當我運行隨機視圖有空

@model Vidly.Models.Movie 

@{ 
    ViewBag.Title = "Random"; 
} 

<h2> 
    @Model.Name 
</h2> 

這可能是空從哪裏來但爲什麼。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Vidly.Models; 

namespace Vidly.Controllers 
{ 
    public class MovieController : Controller 
    { 
     // GET: Movie 
     public ActionResult Random() 
     { 

      var movie = new Movie() { Name = "Shrek!" }; 

      return View(); 
     } 
    } 
} 

這是我的新希望那類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Vidly.Models 
{ 
    public class Movie 
    { 

     public int Id { get; set; } 
     public string Name { get; set; } 

    } 

我解釋好

+2

將模型傳遞給視圖。 '返回查看(電影);' –

回答

4

您需要的模型對象傳遞給視圖。

return View(movie); 
2

您必須發送模型才能查看。否則,它將無法看到你的數據,它會拋出異常。你的情況應該是這樣的:

  public ActionResult Random() 
      { 

       var movie = new Movie() { Name = "Shrek!" }; 

       return View(movie); 
      } 

請閱讀有關View()方法上MSDN。它

通過使用呈現視圖到響應的模型創建一個ViewResult對象。

相關問題