如何创建一个视图有着多种模型MVC 4? [英] how to create a view with multiple models mvc 4?
问题描述
所以我工作在asp.net的MVC 4项目,我在我看来,有一个问题,我想是创建具有2类型不同的充模型的观点,第一种观点(指数)采取的IEnumerable(Models.myModel)第二(用户详细信息)取Models.myModel,这是我的模型code:
so i works in asp.net mvc 4 project and i have a problem in my view, what i want is create a view with 2 differnt type of model,first view (Index) take IEnumerable (Models.myModel) the second (subscriber Details) take Models.myModel, this is my Model code :
public class SubscribersModel
{
[Required]
[DataType(DataType.Text)]
public string name { get; set; }
[Required]
[DataType(DataType.Text)]
[StringLength(maximumLength: 10, MinimumLength = 7)]
public string cin { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime birthday { get; set; }
[DataType(DataType.Text)]
public string birthplace { get; set; }
}
我的控制器code:
public class SubscribersController : Controller
{
private AgencyDbEntities dbcontext = new AgencyDbEntities();
private Subscribe sb = new Subscribe();
public ActionResult Index()
{
var subscribers = from sb in dbcontext.Subscribes select new SubscribersModel { cin = sb.cin, name = sb.name, birthday = (DateTime)sb.birthDay, birthplace = sb.birthPlace };
return View(subscribers);
}
public ActionResult Details(string id,string cin,string name)
{
var subscriber = new SubscribersModel();
IEnumerable<Subscribe> list = from s in dbcontext.Subscribes select s;
foreach (var sb in list)
{
if (sb.cin == id)
{
subscriber.cin = sb.cin;
subscriber.name = sb.name;
subscriber.birthday = (DateTime)sb.birthDay;
subscriber.birthplace = sb.birthPlace;
}
}
return View(subscriber);
}
}
我的索引视图:
@model IEnumerable<_3SDWebProject.Models.SubscribersModel>
@{
ViewBag.Title = "Subscribers";
}
<div class="sidebar">
//here i want to show my details view
</div>
<div class="content" style="width: 700px; margin-left: 250px; height: 545px;margin- top: -30px;">
<h2>Subscribers</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@Styles.Render("~/Content/css")
<script src="@Url.Content("~/Scripts/Table.js")" type="text/javascript"></script>
<table class="altrowstable" id="alternatecolor">
<tr>
<th>
CIN
</th>
<th>
Name
</th>
<th>
birthday
</th>
<th>
birthplace
</th>
<th class="operations">
Operations
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.cin)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.birthday)
</td>
<td>
@Html.DisplayFor(modelItem => item.birthplace)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.cin }, new { @class = "details-logo"})
</td>
</tr>
}
</table>
</div>
我的详细信息视图:
@model _3SDWebProject.Models.SubscribersModel
<fieldset>
<legend>SubscribersModel</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.name)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.name)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.birthday)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.birthday)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.birthplace)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.birthplace)
</div>
</fieldset>
所以请,如果有人有任何想法或解决方案我会很AP preciate:
注:该图片描述我想要什么
so please if someone have any idea or solution i will be very appreciate: NB:this a picture describe what i want
推荐答案
创建包含一个新的,复合视图模型这两种视图模式。
Create a new, compound view model that includes both of these view models.
public class CompoundViewModel
{
public IEnumerable<SubscribersModel> AllSubscribers {get; set;}
public SubscribersModel SelectedSubscriber {get; set;}
}
在理想情况下还拆你的看法变成局部视图并使用 DisplayFor&LT渲染复合型的这两个组成部分纳入其中;&GT;
或 EditorFor&LT;&GT ;
。这样,你可以重用视图一个'SubscriberModel其他地方应用程序,如果你需要它。
Ideally also split your view into partial views and render these two part of your compound model into them using DisplayFor<>
or EditorFor<>
. That way you can reuse the view for a 'SubscriberModel' elsewhere in the application if you need it.
您控制器code也可以通过使用依赖注入框架(例如Autofac)注入那些你正在newing了依赖性得到改善。
Your controller code could also be improved by using a dependency injection framework (e.g. Autofac) to inject those dependencies that you are currently newing up.
另一种选择,因为你使用的是同一型号的列表和详细信息视图,将使用Javascript功能来处理这个客户端,可以手动使用jQuery或允许客户端模型的新的框架之一结合像Knockout.js或Angular.js。
Another alternative, given that you are using the same model for the list and the details view, would be to handle this client-side using Javascript, either manually using jQuery or with one of the newer frameworks that allows client-side model binding like Knockout.js or Angular.js.
这篇关于如何创建一个视图有着多种模型MVC 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!