【问题标题】:ASP.NET MVC Strongly-Type ViewModel - Combine Create/List ViewsASP.NET MVC 强类型 ViewModel - 组合创建/列表视图
【发布时间】:2011-02-25 14:31:15
【问题描述】:

我正在学习 ASP.NET MVC 和 Entity Framework Code First,通过创建一个简单的 Bug/Feature 跟踪系统来学习 LINQ。我想通过让用户在上方提交表单并将提交的内容显示在下方来模仿 Twitter 界面。我不确定如何构造强类型视图和所述模型。我想将我的 Create 和 Index 视图合并到一个视图中,问题是 Create 采用单一类型的 Entry (Pylon.Models.Entry),而 Index 采用 IEnumerable 的 Entry (IEnumerable<Pylon.Models.Entry>)。下面是我的视图模型类和显示视图。我只是从“创建”和“索引”视图复制了由脚手架生成的代码,显然混合不同的模型会导致运行时错误,因此视图被破坏了。我的问题是如何重构视图。

// 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>

任何指针或更好的教程/工作代码都会很棒。

【问题讨论】:

    标签: asp.net-mvc razor viewmodel


    【解决方案1】:

    进行以下更改。

    1. 在视图顶部设置@model EntryViewModel
    2. 对于创建表单,将model =&gt; model.Description 更改为model =&gt; model.Entry.Description,因此model 被替换为model.Entry
    3. 对于列表模板,请进行以下更改。 @foreach (var item in Model.Entries )

    【讨论】:

    • 非常感谢,这正是需要发生的事情。
    【解决方案2】:

    如果那是正确的,我不是。 ASP.NET MVC 3 Viewmodel Pattern 的帖子说您不应该在视图模型中使用模型对象。 也许你会仔细检查?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多