从不同的模型在一个View MVC 3显示数据 [英] MVC 3 Display data from different Models in the one View
问题描述
在MVC 3视图我想要显示单个合同和索赔清单,用户将能够创建一个新的索赔。我创建了一个不同的观点合同和索赔我怎么能合并这让只有一种看法。
In the MVC 3 View I want to display a single Contract and a list of Claims, the user will be able to create a new claim. I have created a different view for Contract and Claims how can I merge these so that there is only one view.
达林,
我如何通过MyViewModel的实例作为视图寻找两个字符串?
How do I pass an instance of MyViewModel as the view is looking for two strings?
public ActionResult Details(int id)
{
MyViewModel myView = new MyViewModel ();
return View(_repository.GetContract(id), myView);
}
感谢
我是用PartialViews HTML,但我不明白如何将模型作为参数传递。
I was using PartialViews Html but I don't see how to pass the model as a parameter.
查看合同
@model Portal.Models.Contracts
@using Portal.Models
Contract View @model Portal.Models.Contracts @using Portal.Models
显示数据。
<div>
@{
Html.RenderPartial("_Claim", Portal.Models.Contracts);
}
</div>
我收到一个错误CS0119:'Models.Contracts'是一个'类型',这是不是在给定的范围内有效。
I am getting an error CS0119: 'Models.Contracts' is a 'type', which is not valid in the given context.
中的数据绑定到合同查看此处
The data is bound to the Contract View here
public ActionResult Details(int id)
{
return View(_repository.GetContract(id);
}
如何绑定到 PartialView _Claim
这类型的的IEnumerable&LT数据; Models.Claims&GT;
推荐答案
您可以定义将包裹这两个模型视图模型:
You could define a view model which will wrap those two models:
public class MyViewModel
{
public Contract Contract { get; set; }
public Claim[] Claims { get; set; }
}
再有你的控制器动作传递MyViewModel的一个实例的视图,这里面查看使用显示模板:
then have your controller action pass an instance of MyViewModel to the view and inside this view use display templates:
@model MyViewModel
@Html.DisplayFor(x => x.Contract)
@Html.DisplayFor(x => x.Claims)
,然后定义显示模板的合同
和索赔
模型(〜/浏览次数/Shared/DisplayTemplates/Contract.cshtml
和〜/查看/共享/ DisplayTemplates / Claim.cshtml
)。
这篇关于从不同的模型在一个View MVC 3显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!