【发布时间】:2013-04-25 15:40:12
【问题描述】:
所以我在 asp.net mvc 4 项目中工作,我的观点有问题,我想要创建一个具有 2 种不同类型模型的视图,第一个视图(索引)采用 IEnumerable(Models.myModel)第二个(订阅者详情)获取 Models.myModel,这是我的模型代码:
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>
如果有人有任何想法或解决方案,我将不胜感激: 注意:这张图片描述了我想要的
【问题讨论】:
-
一个视图应该只与一个模型相关。您可以将两个模型合并为一个,并在视图中使用该组合模型。
-
我不同意这一点。一个视图可以很好地,有时必须,根据需要与尽可能多的模型相关联。这就是为什么我使用我称之为“视图模型”的东西(即使它不是绝对正确的),模型和视图之间的一个类,来构建适当的内存结构,并将其传递给视图。但绝不可以将其命名为“模型”。请记住,如果您使用的是 EF Code First,创建“组合”新模型实际上会在数据库中生成一个新表。我怀疑这就是目标....
标签: asp.net-mvc entity-framework razor asp.net-mvc-4