我需要编辑一个可变长度列表,所以我已经跟着Steven Sanderson's blog和Ivan Zlatev's blog了解了这一点。 型号:
public class TagViewModel
{
[Required]
[StringLength(200)]
[DataType(DataType.Text)]
[Display(Name = "Тип")]
public string Name { get; set; }
[Required]
[StringLength(200)]
[DataType(DataType.Text)]
[Display(Name = "Значение")]
public string Value { get; set; }
[HiddenInput(DisplayValue = false)]
public long Id { get; set; }
[HiddenInput(DisplayValue = false)]
public long Owner { get; set; }
[HiddenInput(DisplayValue = false)]
public SpecTreeNodeType OwnerType { get; set; }
}
查看:
@model IEnumerable<Security.Models.TagViewModel>
@{
ViewBag.Title = "Tag editor";
}
<h2>Tag editor</h2>
@using(Html.BeginForm("EditTags", "TreeView", FormMethod.Post)) {
<div id="editorRows">
@for (int i = 0; i < Model.Count(); i++)
{
Html.RenderPartial("_partialTagEditor", Model.ElementAt(i));
}
</div>
@Html.ActionLink("Add tag", "AddTag", null, new { id = "addItem" });
<input type="submit" value="Save changes" />
}
<script>
$("#editorRows").on("click", "a.deleteRow", function () {
$(this).closest("div.editorRow").remove();
});
$("#addItem").click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) { $("#editorRows").append(html); }
});
return false;
});
</script>
部分观点:
@model Security.Models.TagViewModel
<div class="editorRow">
@using (Html.BeginCollectionItem("TagViewModel"))
{
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
@Html.LabelFor(x => x.Value)
@Html.TextBoxFor(x => x.Value)
@Html.EditorFor(x => x.Id)
@Html.EditorFor(x => x.Owner)
@Html.EditorFor(x => x.OwnerType)
<a href="#" class="deleteRow">delete</a>
}
</div>
邮政行动方法:
[HttpPost]
public ActionResult EditTags(IEnumerable<TagViewModel> tagList)
{
return View("Index");
}
Fiddler表示,EditTags发布请求包含所需数据:
TagViewModel.Index c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c
TagViewModel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].Name Length
TagViewModel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].Value 100 - 200
TagViewModel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].Id 1
TagViewModel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].Owner 3
TagViewModel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].OwnerType value
TagViewModel.Index 9fad7d46-f6f6-4cd7-80be-41e1b5011670
TagViewModel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].Name Width
TagViewModel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].Value 10 - 20
TagViewModel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].Id 2
TagViewModel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].Owner 3
TagViewModel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].OwnerType value
TagViewModel.Index 30048799-116c-4eba-9010-580c681e6a5b
TagViewModel[30048799-116c-4eba-9010-580c681e6a5b].Name wqe
TagViewModel[30048799-116c-4eba-9010-580c681e6a5b].Value qwe
TagViewModel[30048799-116c-4eba-9010-580c681e6a5b].Id 0
TagViewModel[30048799-116c-4eba-9010-580c681e6a5b].Owner 0
TagViewModel[30048799-116c-4eba-9010-580c681e6a5b].OwnerType value
但是在控制器的EditTags操作方法中,tagList为null。我卡住了,不知道如何解决这个问题。
答案 0 :(得分:0)
我看到您重用了blog post中的大量代码。你有一个小错误。当您使用Steven的自定义助手时,您提供了错误的名称:
@using (Html.BeginCollectionItem("TagViewModel"))
但它应该是:
@using (Html.BeginCollectionItem("tagList"))
因为您的EditTags
操作有一个名为tagList
的参数。
您还可以将集合作为主模型的一部分发布。所以你的模型可能看起来像这样:
public class IndexViewModel
{
public IEnumerable<TagViewModel> Tags { get; set; }
}
部分观点:
@using (Html.BeginCollectionItem("Tags"))