C# ASP.NET集合在发布后为空
尽管采纳了我在其他问题和建议中找到的所有建议 传递给视图的列表vsValues在POST后始终为空 查看C# ASP.NET集合在发布后为空,c#,asp.net,post,asp.net-mvc-5,C#,Asp.net,Post,Asp.net Mvc 5,尽管采纳了我在其他问题和建议中找到的所有建议 传递给视图的列表vsValues在POST后始终为空 查看 @model OTS.ParcelOrder @{ ViewBag.Title=“创建”; Layout=“~/Views/Shared/_Layout.cshtml”; } 创造 @使用(Html.BeginForm()) { @Html.AntiForgeryToken() 包订单 @Html.ValidationSummary(true,“,new{@class=“text dang
@model OTS.ParcelOrder
@{
ViewBag.Title=“创建”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
包订单
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.otsID,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.otsID,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.otsID,“,new{@class=“text danger”})
@LabelFor(model=>model.parcelID,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.parcelID,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.parcelID,“,new{@class=“text danger”})
@LabelFor(model=>model.recipientCountry,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.recipientCountry,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.recipientCountry,“,new{@class=“text danger”})
@对于(int i=0;imodel.vsValues[i],new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.vsValues[i],“”,new{@class=“text danger”})
}
}
@ActionLink(“返回列表”、“索引”)
这是因为你没有向任何地方发布任何内容。看看这个
@using (Html.BeginForm())
应该是:
@using (Html.BeginForm("Create","ParcelOrders",FormMethod.Post))
更新
在那之后。在我看来,您的模型是错误的,如果您想将值传递给列表,我建议您有一个相同类型的列表属性[此属性需要在您的模型
ParcelOrder
中,而不是虚拟的]。然后在类的无参数构造函数中执行foreach。在您看到问题的每个步骤中跟踪它 指定POST方法既不会改变情况(列表仍然为空),也不会改变表单的结果HTML代码(已指定有效的HTTP方法和路径)。此外,基本字段(如otsId)也已正确发布。关于模型的问题,您是对的。在空构造函数中填充列表解决了这个问题。