2014-10-20 129 views
0

我是非常新的MVC,並試圖顯示一些數據在視圖中,但無法做到這一點。 我正在Controller類中創建一些虛擬數據(不是從數據庫中選取它)並嘗試在View中查看。我的代碼如下:無法獲取數據查看MVC

控制器代碼:

namespace MvcApplication1.Controllers 
{ 
    public class FirstController : Controller 
    { 
     // 
     // GET: /First/ 

     public ActionResult Index() 
     { 
      var item = new First(1, "ee"); 
      return View(item); 
     } 
    } 
} 

型號代碼:

namespace MvcApplication1.Models 
{ 

    public class First 
    { 
     private string _name; 
     private int _Id; 
     public First(int id, string name) 
     { 
      _Id = id; 
      _name = name; 
     } 
     public string name{ 
      get 
      { 
       return _name; 
      } 
      set 
      { 
       _name= value; 
      } 
     } 

     public int Id 
     { 
      get { return _Id;} 
      set { _Id = value; } 
     } 
    } 
} 

_Layout.cshtml

<ul id = "menu"> 
<li>@Html.ActionLink("Name","Index","First")</li> 
</ul> 

Index.cshtml

@model MvcApplication1.Models.First 
@{ 
    ViewBag.Title = "Index"; 
} 
<h2>Index</h2> 
Hell0 
@{ 
    Html.DisplayFor(model => model.name); 
} 
+0

IMO,沒有理由爲什麼這不應該工作。 – Yasser 2014-10-20 08:05:44

+0

'_Layout.cshtml'中是否存在'@RenderBody()'? – 2014-10-20 08:10:39

+0

在頁面上呈現'Hell0'嗎? – Yasser 2014-10-20 08:17:18

回答

0

嘗試這樣的:

Index.cshtml

@model MvcApplication1.Models.First 

@{ 
    ViewBag.Title = "Index"; 
}, 
<h2>Index</h2> 
Hell0 
@Html.DisplayFor(model => model.name) 

@model MvcApplication1.Models.First 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

@using(Html.BeginForm()) 
{ 
    @:Hello 
    @Html.DisplayFor(model => model.name) 
} 

問候, 馬辛