我是MVC的新手,试图在视图中显示一些数据,但无法做到。 我正在Controller类中创建一些虚拟数据(不是从db中选择)并尝试在View中查看。我的代码如下:
控制器代码:
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 :(得分: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)
}
此致 马尔钦