在控制器post方法中,MVC 4模型中的提交表单为空
所以我现在的问题是,当我提交以下表格时,我无法将我的模型放入我的控制器中。 我试图让BillingCodes(这是一个BillingCodeObjects列表)中的项循环并显示。 我已经删除了一些与这种情况无关的代码,使其更简单,更容易阅读。
这是我的观点代码……
@using (Html.BeginForm("SubmitTimesheet", "Timesheet", FormMethod.Post)) { foreach (var item in Model.BillingCodes) { @item.Name @item.TotalHours Enter Time: @Html.DropDownListFor(model => item.EnterTimeHours, new SelectList(new[] { new { Value = "0", Text = "0" }, new { Value = "1", Text = "1" }, new { Value = "2", Text = "2" }, new { Value = "3", Text = "3" }, new { Value = "4", Text = "4" }, new { Value = "5", Text = "5" }, new { Value = "6", Text = "6" }, new { Value = "7", Text = "7" }, new { Value = "8", Text = "8" }, new { Value = "9", Text = "9" }, new { Value = "10", Text = "10" }, new { Value = "11", Text = "11" }, new { Value = "12", Text = "12" }, }, "Value", "Text")) : @Html.DropDownListFor(model => item.EnterTimeMinutes, new SelectList(new[] { new { Value = "0", Text = "00" }, new { Value = "15", Text = "15" }, new { Value = "30", Text = "30" }, new { Value = "45", Text = "45" }, }, "Value", "Text")) Enter Memo: @Html.TextAreaFor(model => item.Comment)
这是我的控制器的一些代码:
public class TimesheetController : Controller { // // GET: /Timesheet/ public ActionResult Index() { string getBillingCodeUrl =""; //SOME CODE REMOVED FOR LENGTH / READABILITY foreach (var entryItem in timePeriod.TimeEntries[0].EntryCollection) { foreach (var billingItem in billingCodeList.BillingCodes) { if (entryItem.BillingCode == billingItem.Name) { //update record in billingItem with data from entryItem billingItem.Comment = entryItem.Comment; billingItem.Billable = entryItem.Billable; billingItem.TotalHours = entryItem.Hours; } } } return View(billingCodeList); } [HttpPost] public void SubmitTimesheet(BillingCodeList model) { string uri = ""; foreach (var billingCode in model.BillingCodes) { //do stuff with each of these } } } }
最后,这是模型中的信息:
public class BillingCodeList { public List BillingCodes; } public class BillingCodeObj { public string Name { get; set; } public decimal TotalHours { get; set; } public decimal EnterTimeHours { get; set; } public decimal EnterTimeMinutes { get; set; } public bool Billable { get; set; } public string Comment { get; set; } public BillingCodeObj(string name, decimal hours) { this.Name = name; this.TotalHours = hours; } public BillingCodeObj() { } }
这是返回表单时调试本地的图片。
当地人的形象
你正在对视图做一个foreach,所以输入元素应该在这里发布的值如下: name="BillingCodes[0].EnterTimeHours"
, name="BillingCodes[0].EnterTimeMinutes"
你可以在网络中检查在Chrome开发者工具(CTRL + SHIFT + C)上标记请求或仅查看来源。 如果是这种情况,您将提交多个BillingCodeObj对象。 你必须有一个接收它的控制器。
看看源代码,因为这可以极大地帮助您了解幕后发生的事情。
在您的控制器上试试这个:
[HttpPost] public void SubmitTimesheet(IEnumerable billingCodes){ }
您也可以(出于调试目的)这样做
public void SubmitTimesheet(FormCollection form){}
并检查表单在调试时如何填充。
在评论和更多代码提供后,您将视图更改为:
@using (Html.BeginForm("SubmitTimesheet", "Timesheet", FormMethod.Post)) { @Html.EditorFor(m=>m.BillingCodes) }
在EditorTemplates / BillingCodeObj.cshtml中创建一个新的cshtml:
@model BillingCodeObj @Model.Name @Model.TotalHours Enter Time: @Html.DropDownListFor(model => model.EnterTimeHours, new SelectList(new[] { new { Value = "0", Text = "0" }, new { Value = "1", Text = "1" }, new { Value = "2", Text = "2" }, new { Value = "3", Text = "3" }, new { Value = "4", Text = "4" }, new { Value = "5", Text = "5" }, new { Value = "6", Text = "6" }, new { Value = "7", Text = "7" }, new { Value = "8", Text = "8" }, new { Value = "9", Text = "9" }, new { Value = "10", Text = "10" }, new { Value = "11", Text = "11" }, new { Value = "12", Text = "12" }, }, "Value", "Text")) : @Html.DropDownListFor(model => model.EnterTimeMinutes, new SelectList(new[] { new { Value = "0", Text = "00" }, new { Value = "15", Text = "15" }, new { Value = "30", Text = "30" }, new { Value = "45", Text = "45" }, }, "Value", "Text")) Enter Memo: @Html.TextAreaFor(model => model.Comment)
您的View是BillingCodeList
类型,但您尝试将List
发送到控制器中的操作。
尝试按如下方式更改操作:
[HttpPost] public void SubmitTimesheet(BillingCodeList model) { string uri = ""; foreach (var billingCode in model.BillingCodes) { //do stuff with each of these } }
编辑:在将模型发送到视图之前,您似乎没有将BillingCodeList的列表元素初始化为new List()
。 或者是你?
- ASP.NET MVC 3 WebGrid分页问题
- 为什么在Visual Studio 2015专业版中使用Entity Framework进行映射无效
- DataAnnotation属性伙伴类奇怪 – ASP.NET MVC
- 为什么模型绑定器需要一个空构造函数
- 使用Razor在POST上使用Model.List为null
- 在razor视图中使用Html.DisplayFor应用css类
- 使用自定义DataAnnotationsModelValidatorProvider进行服务器端validation
- 流zip文件MVC.NET开始流式传输
- ASP.Net MVC Jquery UI自动完成 – 我调试时没有显示列表
Interesting Posts