【问题标题】:How to add a list to a View Model MVC如何将列表添加到视图模型 MVC
【发布时间】:2012-07-19 19:12:46
【问题描述】:

我正在使用带有 View Model 的 MVC 3,在我的例子中,我有一个 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; }
    }
}

查看

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

【问题讨论】:

  • 在您看来,您有一个创建新评论的表单和一个显示所有 cmets 的表格,是吗?你能把你的控制器代码吗?

标签: asp.net-mvc c#-4.0 viewmodel


【解决方案1】:

您不需要将Collection 传递给您的视图。 ViewModel 的一个对象就足够了。您的ViewModel 已经拥有收藏该收藏的财产(Comments 列表)

因此,在您的 GET 操作中,仅将此 viewModel 的一个实例返回给视图

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

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

@model TestGuestBook.ViewModel.ListAddCommentsViewModel

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

@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>
}

【讨论】:

  • 那么,如果我想添加一个新的评论,我应该如何在这里显示 html
【解决方案2】:

这是最好的解决方案,我检查了几乎所有信息,这是一个有效的解决方案!谢谢

    public  ActionResult UploadFile(UploadFileViewModel model)
    {
        if (ModelState.IsValid)
        {
            var file = model.File;
            var parsedContentDisposition =
            ContentDispositionHeaderValue.Parse(file.ContentDisposition);
            var filename = Path.Combine(_environment.WebRootPath,
                "Uploads", parsedContentDisposition.FileName.Trim('"'));
            file.SaveAsAsync(filename);
        }

        return View();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多