我是 ASP.NET MVC 的新手。我无法清楚地理解它们之间的区别
Strongly Typed view vs Normal View vs Partial View vs Dynamic-type View
在 Asp.NET MVC 中。任何人都向我描述了这个条款。
提前致谢!!!
最佳答案
Strongly Typed view
绑定(bind)到 View 模型的 View 。例如,如果您有以下 View 模型:
public class MyViewModel
{
public string SomeProperty { get; set; }
}
由 Controller Action 传递给 View :
public ActionResult Index()
{
var model = new MyViewModel();
model.SomeProperty = "some property value";
return View(model);
}
强类型 View 顶部的@model
指令指向这个 View 模型:
@model MyViewModel
...
<div>@Model.SomeProperty</div>
Partial View
View 和分部 View 的区别在于,分部 View 只包含一些小的 HTML 片段,可以在普通 View 的多个部分中重复使用。例如,您可以定义以下分部 View :
@model AddressViewModel
<div>Street: @Model.Street</div>
<div>Country: @Model.Country</div>
然后在主视图中的多个位置呈现此部分 View ,以避免一遍又一遍地重复相同的代码:
@model MainViewModel
...
<h3>Personal address</h3>
<div>@Html.Partial("_Address.cshtml", Model.PersonalAddress)</div>
...
<h3>Business address</h3>
<div>@Html.Partial("_Address.cshtml", Model.BusinessAddress)</div>
Dynamic-type View
没有模型的 View 或使用ViewBag
等弱类型结构的 View 。例如,您可以有一个 Controller 操作,它在 ViewBag
中设置一些属性:
public ActionResult Index()
{
ViewBag["SomeProperty"] = "some property value";
return View();
}
和相应的 View ,您可以使用 ViewBag 中的相同键访问此属性:
<div>@ViewBag["SomeProperty"]</div>
关于c# - Asp.NET MVC 中的强类型 View 与普通 View 与局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089268/