如何将列表添加到View Model MVC

Gib*_*boK 2 asp.net-mvc viewmodel c#-4.0

我正在使用MVC 3和View Model,在我的例子中我有一个View Model,它应该显示一个项目列表,还有一个用于插入一些输入的表单.

我的视图中存在问题,因为我无法将表格与视图模型相关联,您能告诉我我做错了什么吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using TestGuestBook.Models;

namespace TestGuestBook.ViewModel
{
    public class ListAddCommentsViewModel
    {
        public int CommentId { get; set; }

        [Required]
        public string Nominative { get; set; }

        [Email]
        public string Email { get; set; }

        [Required]
        public string Content { get; set; }

        public List<Comment> CommentItems { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

视图

@model IEnumerable<TestGuestBook.ViewModel.ListAddCommentsViewModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ListAddCommentsViewModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CommentId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CommentId)
            @Html.ValidationMessageFor(model => model.CommentId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Nominative)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Nominative)
            @Html.ValidationMessageFor(model => model.Nominative)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Content)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Content)
            @Html.ValidationMessageFor(model => model.Content)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            CommentId
        </th>
        <th>
            Nominative
        </th>
        <th>
            Email
        </th>
        <th>
            Content
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CommentId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Nominative)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Content)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>
Run Code Online (Sandbox Code Playgroud)

Shy*_*yju 9

你不需要传递Collection给你的视图.只有ViewModel的一个对象就足够了.你ViewModel已经拥有收藏品的财产(清单Comments)

因此,在您的GETAction中,只返回此viewModel的一个实例到View

public ActionResult GetComments(int postId)
{
  var viewModel=new ListAddCommentsViewModel();
  viewModel.CommentItems =db.GetComments(postId);
  return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)

现在在你的视图中,让它绑定到一个单独的实例 ListAddCommentsViewModel

@model TestGuestBook.ViewModel.ListAddCommentsViewModel
Run Code Online (Sandbox Code Playgroud)

在您的视图中,要显示您的注释列表,请使用您的收集类型属性(Model.CommentItems)ViewModel

@foreach (var item in Model.CommentItems) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CommentId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Nominative)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Content)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new {  @id=item.PrimaryKey  }) |
            @Html.ActionLink("Details", "Details", new {  @id=item.PrimaryKey  }) |
            @Html.ActionLink("Delete", "Delete", new { @id=item.PrimaryKey  })
        </td>
    </tr>
}
Run Code Online (Sandbox Code Playgroud)