asp.net-mvc 如何将列表添加到视图模型 MVC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11567723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a list to a View Model MVC
提问by GibboK
I'm using MVC 3 with View Model, in my case I have a View Model that should display a list of items and also a form for inserting some input.
我将 MVC 3 与视图模型一起使用,在我的情况下,我有一个视图模型,它应该显示一个项目列表以及一个用于插入一些输入的表单。
I have problem in my View because I'm not able to associate the Form for inserting the data with the view model, could you tell me what I'm doing wrong?
我的视图有问题,因为我无法将用于插入数据的表单与视图模型相关联,你能告诉我我做错了什么吗?
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; }
}
}
View
看法
@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>
回答by Shyju
You dont need to pass a Collectionto your View. Only one object of the ViewModel is enough. Your ViewModelalready have property to holde the collection (List of Comments)
您不需要将 a 传递Collection给您的视图。ViewModel 的一个对象就足够了。您ViewModel已经拥有财产来持有该集合(列表Comments)
So in your GETAction, return only one instance of this viewModel to the View
所以在你的GETAction 中,只将这个 viewModel 的一个实例返回给 View
public ActionResult GetComments(int postId)
{
var viewModel=new ListAddCommentsViewModel();
viewModel.CommentItems =db.GetComments(postId);
return View(viewModel);
}
and now in your View, Let it bind to a single instance of ListAddCommentsViewModel
现在在您的视图中,让它绑定到单个实例 ListAddCommentsViewModel
@model TestGuestBook.ViewModel.ListAddCommentsViewModel
And Inside your view, to Show your List of comments, use the Collection type property (Model.CommentItems) in your ViewModel
在您的视图中,要显示您的评论列表,请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>
}
回答by Latyn
This is the best solution, I had checked almost all information and this is the one working!! Thanks
这是最好的解决方案,我检查了几乎所有信息,这是一个有效的!谢谢
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();
}

