C# 编辑可变长度列表。视图模型IEnumerable属性从post方法提交空值
我需要编辑一个可变长度的列表,所以我遵循并得出了这个结论。 型号: 视图:C# 编辑可变长度列表。视图模型IEnumerable属性从post方法提交空值,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我需要编辑一个可变长度的列表,所以我遵循并得出了这个结论。 型号: 视图: 但在控制器的EditTags操作方法中,tagList为null。我被卡住了,不知道如何解决这个问题。我看到您重用了大量的代码。你有一个小错误。使用Steven的自定义帮助程序时,您为其提供了错误的名称: @using (Html.BeginCollectionItem("TagViewModel")) 但它应该是: @using (Html.BeginCollectionItem("tagList")) 因为Edi
但在控制器的EditTags操作方法中,tagList为null。我被卡住了,不知道如何解决这个问题。我看到您重用了大量的代码。你有一个小错误。使用Steven的自定义帮助程序时,您为其提供了错误的名称:
@using (Html.BeginCollectionItem("TagViewModel"))
但它应该是:
@using (Html.BeginCollectionItem("tagList"))
因为EditTags操作有一个名为tagList的参数
也可以将集合作为主模型的一部分发布。因此,您的模型可能如下所示:
public class IndexViewModel
{
public IEnumerable<TagViewModel> Tags { get; set; }
}
您的模型没有名为TagViewModel的属性。您需要生成名称为[xxxx].Name[xxxx].Id等。它需要是@使用Html。BeginCollectionItem@StephenMuecke不为我工作。Fiddler显示。索引fdc9158c-6c45-427f-bbd9-3b9add9c896f[fdc9158c-6c45-427f-bbd9-3b9add9c896f]。名称长度。但是仍然不走运。请尝试使用Html.BeginCollectionItemtagList,因为这是您的参数名称,不确定助手为什么要添加。尽管[fdc9158c-6c45-427f-bbd9-3b9add9c896f]仍要索引。名称为correct@StephenMuecke它起作用了!谢谢!
[HttpPost]
public ActionResult EditTags(IEnumerable<TagViewModel> tagList)
{
return View("Index");
}
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
@using (Html.BeginCollectionItem("TagViewModel"))
@using (Html.BeginCollectionItem("tagList"))
public class IndexViewModel
{
public IEnumerable<TagViewModel> Tags { get; set; }
}
@using (Html.BeginCollectionItem("Tags"))