所以我在asp.net mvc 4項目中工作,我在我的視圖中有一個問題,我想要的是創建一個視圖與2不同類型的模型,第一視圖(索引)採取IEnumerable(Models.myModel)第二個用戶詳細信息)採取Models.myModel,這是我的型號代碼:如何用多個模型創建視圖mvc 4?
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; }
}
我的控制器代碼:
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>
所以請,如果有人有任何想法或解決方案,我將非常感激: 注:此圖片描述我想要什麼
視圖只應與一個模型相關。您可以將兩個模型合併爲一個模型,並在視圖中使用該組合模型。 – Flavia 2013-04-25 16:15:38
我不同意這一點。一個觀點可以很好地並且有時必須與需要的模型相關聯。這就是爲什麼我使用我稱之爲'視圖模型'(即使它不是絕對正確)的東西,模型和視圖之間的類,以構建適當的內存結構,並將其傳遞給視圖。但絕不可將其命名爲「模型」。請記住,如果您使用的是EF Code First,那麼創建一個新的「組合」模型實際上會導致數據庫中出現一個新表。我懷疑這是目標.... – BernardG 2013-09-26 16:01:50