在视图中渲染部分视图(简单示例)
<div>
@{Html.RenderPartial("MyPartialViewName",
new { firstName = model.FirstName, lastName = model.LastName});
}
</div>
<div>
@{Html.RenderPartial("MyPartialViewName","MyController",
new { firstName = model.FirstName, lastName = model.LastName});
}
</div>
创建参数视图操作
[ChildActionOnly]
public ActionResult MyPartialViewName(string firstName, string lastName)
{
// create model here...
var model = repository.GetThingByParameter(firstName,lastName);
var partialViewModel = new PartialViewModel(model);
return PartialView(mypartialViewModel);
}
示例视图代码:
<p>
@model.something
</p>
</br>
<span>
@model.otherthing
</span>
或者在局部视图中做所有标记:
<div>
<p>
@model.something
</p>
</br>
<span>@model.otherthing</span>
</div>
当前页面:
@foreach(var Document in model)
{
@{Html.RenderPartial("MyPartialViewName",
new { firstName = model.FirstName, lastName = model.LastName});
}
}
请注意,您还可以将模型部分作为文档传递:
@foreach(var Document in model)
{
@{Html.RenderPartial("MyPartialViewName",Document);
}
和局部视图:(这里没有传递参数......只是模型)
@model YourApp.Model.Document
<div>
<p>
@Model.Details
</p>
</br>
<span>@Model.File</span>
</div>