结合不同模型的两个视图
大家好,我不知道该怎么做。我有两个视图和两个独立的模型,我想将这两个视图合并起来,以便它们都在一个视图上。
视图 1:
@model IEnumerable<TelephoneNumberManagement.Models.Range>
<table>
<tr>
<th>
RangeName
</th>
<th>
RangeNumber
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink(item.RangeName, "ViewSubRange", new { id = item.ID })
</td>
<td>
@item.RangeNumber
</td>
</tr>
}
</table>
视图 2:-
@model IEnumerable<TelephoneNumberManagement.Models.TestNumber>
<h2>Index</h2>
<table>
<tr>
<th>
Number
</th>
<th>
Status
</th>
<th>
Customer
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Number</td>
<td>@item.Status.StatusName</td>
<td>@item.CustomerID</td>
</tr>
}
</table>
HomeController:-
public ViewResult Index()
{
return View(context.Ranges.ToList().OrderBy(m => m.RangeName));
}
public ActionResult ViewSubRange(int id)
{
IEnumerable<TestNumber> testNumbersList = context.TestNumbers.Where(m => m.RangeID == id).ToList();
return View("SubRange", testNumbersList);
}
任何帮助将非常感激,因为我正在用这个把头撞在墙上!
Hi all i'm not sure how to do this. I have two views with 2 separate models, i would like to combine the 2, so that they are both on one view.
View 1:
@model IEnumerable<TelephoneNumberManagement.Models.Range>
<table>
<tr>
<th>
RangeName
</th>
<th>
RangeNumber
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink(item.RangeName, "ViewSubRange", new { id = item.ID })
</td>
<td>
@item.RangeNumber
</td>
</tr>
}
</table>
View 2:-
@model IEnumerable<TelephoneNumberManagement.Models.TestNumber>
<h2>Index</h2>
<table>
<tr>
<th>
Number
</th>
<th>
Status
</th>
<th>
Customer
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Number</td>
<td>@item.Status.StatusName</td>
<td>@item.CustomerID</td>
</tr>
}
</table>
HomeController:-
public ViewResult Index()
{
return View(context.Ranges.ToList().OrderBy(m => m.RangeName));
}
public ActionResult ViewSubRange(int id)
{
IEnumerable<TestNumber> testNumbersList = context.TestNumbers.Where(m => m.RangeID == id).ToList();
return View("SubRange", testNumbersList);
}
Any help would be really appreciated as i'm banging my head against the wall with this!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
获取两个视图并将它们放入局部视图中(如果您仍然需要原始视图,请让它们也使用局部视图,以便您的视图定义在一处 - 局部视图)。对于“两者都在一个视图中”,添加一个新操作和一个模型,该模型具有部分所需的每个模型的实例。
模型:
操作:
视图 - 添加一个视图,并调用为每个视图渲染部分:
或者您可以使用 ViewData/ViewBag 来避免创建 MyBothInOneModel 类。
Take your two views and put them into partials (if you still need the original views have them also use the partials so your views are defined in one place -- the partials). For your "both in one view", add a new action and a model that has an instance of each model needed for the partials.
Model:
Action:
View - add a view with a call to render partial for each:
Or you can use ViewData/ViewBag to avoid creating MyBothInOneModel class.
你有很多选择。首先,您可以创建一个具有属性
IEnumerable的 ViewModel TestNumbers
和属性
IEnumerableRanges
,然后返回一个视图,绑定到新的 ViewModel 并使用组合数据。
作为替代方案(恕我直言,更智能的解决方案),您可以将每个视图渲染为子视图
@Html.Action("ActionForView1")
和@Html.Action("ActionForView2")
。You have a bunch of options. First you can create a ViewModel having a property
IEnumerable<TelephoneNumberManagement.Models.TestNumber> TestNumbers
and a property
IEnumerable<TelephoneNumberManagement.Models.Range> Ranges
and then return a view, binding to the new ViewModel and using the combined data.
As an alternative (imho smarter solution) you could render each view as a child view
@Html.Action("ActionForView1")
and@Html.Action("ActionForView2")
.您可以使用 ViewModel 模式来解决这个问题。创建一个“ViewModel”,它结合了给定视图所需的所有不同模型和/或表示逻辑。
例如:
然后让您的新组合视图使用 ViewModel 作为其模型。
You can use the ViewModel pattern to solve this. Create a "ViewModel" that combines all the different models and/or presentation logic you need for a given view.
e.g.:
Then let your new combined view use the ViewModel for it's model.
另一种选择是为视图使用动态变量
然后您可以将所需的任何模型发送到视图,但您需要明智地了解要使用的属性。
Another option is to use a dynamic variable for your views
Then you could send any model you want to the view, but you would need to be wise about what properties to use.
这是我正在谈论的一个基本示例。
在我的登录视图上,我使用 @model Web.ViewModels.LoginRegisterMemberViewModel
部分视图也使用 @model Web.ViewModels.LoginRegisterMemberViewModel
所以现在我可以访问 LoginRegsiterMemberViewModel.SiteLoginViewModel.property 等。
希望这并不完全偏离您想要完成的目标。
Here is a basic example of what I am talking about.
On my Login View I user @model Web.ViewModels.LoginRegisterMemberViewModel
The partial views also use @model Web.ViewModels.LoginRegisterMemberViewModel
So now I have access to LoginRegsiterMemberViewModel.SiteLoginViewModel.property etc..
Hope this is not totally off base from what you are trying to accomplish.
如果您希望获得干净的分离,另一个不错的选择是从主视图使用 Html.RenderAction。然后为所描述的每个视图实现单独的控制器。
Also a good option, if you wish to obtain clean separation, is to use Html.RenderAction from a main view. Then implement to separate controllers for each of the views described.