2012-07-19 84 views
2

我正在使用MVC 3与视图模型,在我的情况下,我有一个视图模型,应该显示项目的列表,还有一个表单插入一些输入。如何添加列表到视图模型MVC

我在我的视图中有问题,因为我无法将表单插入数据与视图模型,你能告诉我我做错了什么吗?

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> 
+0

在你看来,你有一个表格来创建一个新的评论和一张表来显示所有的评论,你呢?你可以把你的控制器代码? – 2012-07-19 19:21:32

回答

8

你不需要一个Collection传递给你的视图。只有ViewModel的一个对象就足够了。你ViewModel已经拥有财产holde集合(的Comments列表)

因此,在你GET行动,返回此视图模型只有一个实例来查看

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

,现在,让我们把它绑定到ListAddCommentsViewModel

@model TestGuestBook.ViewModel.ListAddCommentsViewModel 

而且里面的视图中的单个实例,以显示您的评论列表,在你使用集合类型属性(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> 
} 
+0

所以,如果我想添加一个新的coment,我应该如何显示这里的html – senK 2015-11-28 09:09:56

0

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

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(); 
    } 
+0

使用视图模型的窍门! – Latyn 2016-05-12 06:20:14

相关问题