2014-10-20 130 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) 
} 

问候, 马辛