如何创建一个视图模型以列表的一个编辑观点 [英] How to create an Edit view for a ViewModel with lists
问题描述
我是相当新的MVC,所以我敢肯定,我做错了什么。我尽我所能去做,而不是回到原来只是HTML起草它全部由手工所有的MVC方式。
I'm fairly new to MVC, so I'm sure I'm doing something wrong. I'm trying my best to do it all the MVC way instead of reverting back to just crafting it all by hand in HTML.
下面是我的编辑视图
@model NotesBoard.Models.RoleViewModel
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>RoleViewModel</legend>
@Html.HiddenFor(model => model.Role.Name)
<div class="editor-label">
@Html.LabelFor(model => model.Role.Name)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.Role.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Role.Users)
</div>
<div class="editor-field">
<table>
<tr>
<td class="remove-td">Tick to Remove</td>
<td></td>
<td class="add-td">Tick to Add</td>
</tr>
<tr>
<td style="vertical-align:top; margin: 0px;">
<table style="margin: 0px;">
@foreach (var item in Model.Role.Users)
{
<tr>
<td class="remove-td">
@Html.CheckBoxFor(modelItem => item.State)
</td>
<td>
@Html.DisplayFor(modelItem => item.User)
</td>
</tr>
}
</table>
</td>
<td></td>
<td style="vertical-align:top; margin:0px;">
<table style="margin: 0px;">
@foreach (var item in Model.Users)
{
Boolean RoleUser = Model.Role.Users.Exists(model => model.User == item.User);
<tr>
@if(RoleUser)
{
<td class="add-td">
@Html.CheckBoxFor(modelItem => item.State, new { disabled=true })
</td>
<td class="disabled-label">
@Html.DisplayFor(modelItem => item.User)
</td>
}
else
{
<td class="add-td">
@Html.CheckBoxFor(modelItem => item.State, new { id=item.User })
</td>
<td>
@Html.DisplayFor(midelItem => item.User)
</td>
}
</tr>
}
</table>
</td>
</tr>
</table>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
和这里是我的模型:
public class AccountUser
{
public AccountUser() { }
public AccountUser(Int32 id, String user)
{
Id = id;
User = user;
}
[Key]
public Int32 Id { get; set; }
public String User { get; set; }
}
public class AccountUserViewModel : AccountUser
{
public AccountUserViewModel(Int32 id, String user, Boolean State = false)
{
Id = id;
User = user;
}
public Boolean State { get; set; }
}
public class RoleModel
{
public String Name { get; set; }
public List<AccountUserViewModel> Users { get; set; }
}
public class RoleViewModel
{
public RoleModel Role { get; set; }
public List<AccountUserViewModel> Users { get; set; }
}
当我从编辑视图得到回传两个用户列表是空的。
When I get the postback from the edit view both the user lists are null.
下面是在我的控制器Post方法至今:
Here is the Post method in my controller so far:
[HttpPost]
[Authorize(Roles = "Admin")]
public ActionResult Edit(RoleViewModel model)
{
if (ModelState.IsValid)
{
var data = Request.Form;
return RedirectToAction("Index");
}
return View(model);
}
如果我尝试直接通过使用Request.Form集合中提取数据,该复选框都被命名为item.state。我应该如何落实这一观点得到血统的数据备份,而不恢复到基本上只是各具特色平面形式旧的HTML自己?
If I try to extract the data directly by using the Request.form collection, the checkboxes are all named "item.state". How should I be implementing this view to get descent data back without reverting to basically just crafting the form in plane old HTML myself?
推荐答案
更改的foreach
在名单的
循环使索引可以在LAMDA使用,并由的Razor视图正确呈现:
Change the foreach
over the lists to FOR
loops so that the index can be used in the lamda and rendered correctly by the Razor view:
@for (int i = 0; i < Model.Users.Count(); i++) {
{
<tr>
@if(RoleUser)
{
<td class="add-td">
@Html.CheckBoxFor(modelItem => Model.Users[i].State, new { disabled = true })
</td>
<td class="disabled-label">
@Html.DisplayFor(modelItem => Model.Users[i].User)
</td>
...
...
模型绑定然后将数据绑定到正确的名单。
The model binder will then bind the data to the lists correctly.
看看这个答案的SO: http://stackoverflow.com/a/6585642/1099260
Check out this SO answer: http://stackoverflow.com/a/6585642/1099260
这篇关于如何创建一个视图模型以列表的一个编辑观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!