ASP.NET MVC强类型ViewModel-组合创建/列表视图 [英] ASP.NET MVC Strongly-Type ViewModel - Combine Create/List Views
问题描述
我正在通过创建一个简单的Bug/功能跟踪系统来学习ASP.NET MVC和Entity Framework Code First(LINQ).我想通过让用户在上方提交一个表单并在下方显示所提交的内容来模仿Twitter界面.我不确定如何构造强类型视图和所述模型.我想将我的创建"和索引"视图合并到一个视图中,问题是创建"采用单一类型的Entry(Pylon.Models.Entry
),而索引"则采用IEnumerable of Entry(IEnumerable<Pylon.Models.Entry>
).下面是我的viewmodel类和Display视图.我只是从创建"和索引"视图中复制了通过脚手架生成的代码,显然混合使用不同的模型会导致运行时错误,因此视图被破坏了.我的问题是如何重构视图.
I am learning ASP.NET MVC and Entity Framework Code First, LINQ by creating a simple Bug/Feature tracking system. I would like to mimic the Twitter interface by having the user submit a form above and having the submitted content appear below. I am not sure how to structure the strongly-typed view and the said model. I would like to merge my Create and Index views into a single view, the problem is the Create takes a single type Entry (Pylon.Models.Entry
), while the Index takes IEnumerable of Entry (IEnumerable<Pylon.Models.Entry>
). Below is my viewmodel class and Display view. I simply copied the code generated by scaffolding from the "Create" and "Index" views obviously mixing the different models causes run-time errors so the view is broken. My question is how do I restructure the view.
// Entry ViewModel
public class EntryViewModel
{
public Entry Entry { get; set; }
public IEnumerable<Entry> Entries { get; set; }
}
@* Display View *@
@model ?
@{
ViewBag.Title = "Display";
}
<hr />
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Entry</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OpenDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OpenDate)
@Html.ValidationMessageFor(model => model.OpenDate)
</div>
<div class="editor-label">
Paradigm
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ParadigmId, ((IEnumerable<Pylon.Models.Paradigm>)ViewBag.PossibleParadigms).Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.Name),
Value = option.ParadigmId.ToString(),
Selected = (Model != null) && (option.ParadigmId == Model.ParadigmId)
}), "Choose...")
@Html.ValidationMessageFor(model => model.ParadigmId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<hr />
<table>
<tr>
<th></th>
<th>
Description
</th>
<th>
OpenDate
</th>
<th>
Type
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@item.Description
</td>
<td>
@String.Format("{0:d}", item.OpenDate)
</td>
<td>
@(item.Paradigm == null ? "None" : item.Paradigm.Name)
</td>
</tr>
}
</table>
任何指针或更好的教程/工作代码都很棒.
Any pointers or better yet tutorials/working code would be great.
推荐答案
进行以下更改.
- 在视图顶部设置
@model EntryViewModel
. - 对于创建表单,将
model => model.Description
更改为model => model.Entry.Description
,以便将model
替换为model.Entry
- 对于列表模板,进行以下更改.
@foreach (var item in Model.Entries )
- set
@model EntryViewModel
at top of the view. - For create form, change
model => model.Description
tomodel => model.Entry.Description
, somodel
is replaced withmodel.Entry
- For listing template, do the following change.
@foreach (var item in Model.Entries )
这篇关于ASP.NET MVC强类型ViewModel-组合创建/列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!