Combing two views with different models
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.Customer开发者_如何转开发ID</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!!
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:
public class MyBothInOneModel { public ModelA modelA { get; set; } public ModelB modelB { get; set; } }
Action:
public ViewResult BothInOne(int idForB) { MyBothInOneModel m = new MyBothInOneModel(); m.modelA = context.Ranges.ToList().OrderBy(m => m.RangeName)); m.modelB = context.TestNumbers.Where(m => m.RangeID == idForB).ToList(); }
View - add a view with a call to render partial for each:
... Html.RenderPartial(Model.modelA, "PartialA"); Html.RenderPartial(Model.modelB, "PartialB"); ...
Or you can use ViewData/ViewBag to avoid creating MyBothInOneModel class.
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")
.
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.:
class TelephoneViewModel
{
public IEnumerable<TelephoneNumberManagement.Models.TestNumber> { get; set;}
public IEnumerable<TelephoneNumberManagement.Models.Range> { get; set; }
}
Then let your new combined view use the ViewModel for it's model.
Another option is to use a dynamic variable for your views
@model IEnumerable<dynamic>
Then you could send any model you want to the view, but you would need to be wise about what properties to use.
Here is a basic example of what I am talking about.
public class LoginRegisterMemberViewModel
{
public OpenIdLoginViewModel OpenIdLoginViewModel { get; set; }
public SiteLoginViewModel SiteLoginViewModel { get; set; }
public RegisterMemberViewModel RegisterMemberViewModel { get; set; }
public string ReturnUrl { get; set; }
}
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.
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.
精彩评论