当前位置: 首页 > 编程语言 > C#

controllerpost方法中,MVC4模型中的提交表单为空分享

时间:2023-04-10 13:59:20 C#

controllerpost方法中,MVC4模型中的提交表单为空所以我现在的问题是,提交的时候我遵循表格时无法将我的模型放入我的控制器中。我试图让BillingCodes(这是BillingCodeObjects列表)中的项目循环显示。我删除了一些与本案例无关的代码,以使其更简单易读。这是我的观点代码……@using(Html.BeginForm("SubmitTimesheet","Timesheet",FormMethod.Post)){foreach(variteminModel.BillingCodes){@item.Name@item.TotalHours输入时间:@Html.DropDownListFor(model=>item.EnterTimeHours,newSelectList(new[]{new{Value="0",Text="0"},new{Value="1",Text="1"},new{值=“2”,文本=“2”},新{值=“3”,文本=“3”},新{值=“4”,文本=“4”},新{值=“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,newSelectList(new[]{new{Value="0",Text="00"},new{Value="15",Text="15"},new{Value="30",Text="30"},new{Value="45",Text="45"},},"价值","Text"))Billable@Html.CheckBoxFor(model=>item.Billable)EnterMemo:@Html.TextAreaFor(model=>item.Comment)这是我的控制器的一些代码:publicclassTimesheetController:Controller{///GET:/Timesheet/publicActionResultIndex(){stringgetBillingCodeUrl="";//为了长度/可读性删除了一些代码foreach(varentryItemintimePeriod.TimeEntries[0].EntryCollection){foreach(varbillingIteminbillingCodeList.BillingCodes){if(entryItem.BillingCode==billingItem.Name){//更新记录在billingItem中,数据来自entryItembillingItem.Comment=entryItem.Comment;billingItem.Billable=entryItem.Billable;billingItem.TotalHours=entryItem.Hours;}}}返回视图(billingCodeList);}[HttpPost]publicvoidSubmitTimesheet(BillingCodeListmodel){stringuri="";foreach(varbillingCodeinmodel.BillingCodes){//用这些做事}}}}最后,这是模型中的信息:publicclassBillingCodeList{publicListBilling代码;}publicclassBillingCodeObj{publicstringName{get;放;}publicdecimalTotalHours{get;放;}publicdecimalEnterTimeHours{get;放;}publicdecimalEnterTimeMinutes{get;;}publicstringComment{get;放;}publicBillingCodeObj(stringname,decimalhours){this.Name=name;this.TotalHours=小时数;}publicBillingCodeObj(){}}这是一张在返回表单时调试本地的图片你正在视图上做一个foreach的本地图像,所以输入元素应该在这里发布值,如:name="BillingCodes[0].EnterTimeHours",name="BillingCodes[0].EnterTimeMinutes"您可以在网络中检查标记请求或仅在Chrome开发人员工具(CTRL+SHIFT+C)上查看源代码。如果是这种情况,您将提交多个BillingCodeObj对象。您必须有一个接收它的控制器。查看源代码,因为这可以极大地帮助您了解幕后发生的事情。在您的控制器上尝试此操作:[HttpPost]publicvoidSubmitTimesheet(IEnumerablebillingCodes){}您也可以(出于调试目的)执行此操作publicvoidSubmitTimesheet(FormCollectionform){}并在调试时检查表单的填充方式。在提供评论和更多代码后,您将视图更改为:@using(Html.BeginForm("SubmitTimesheet","Timesheet",FormMethod.Post)){@Html.EditorFor(m=>m.BillingCodes)}在创建EditorTemplates/BillingCodeObj.cshtml中的新cshtml:@modelBillingCodeObj@Model.Name@Model.TotalHours输入时间:@Html.DropDownListFor(model=>model.EnterTimeHours,newSelectList(new[]{new{Value="0",文本=“0”},新{值=“1”,文本=“1”},新{值=“2”,文本=“2”},新{值=“3”,文本=“3”},new{Value="4",Text="4"},new{Value="5",Text="5"},new{Value="6",Text="6"},new{Value=“7”,文本=“7”},新{值=“8”,文本=“8”},新{值=“9”,文本=“9”},新{值=“10”,Text="10"},new{Value="11",Text="11"},new{Value="12",Text="12"},},"Value","Text")):@Html.DropDownListFor(model=>model.EnterTimeMinutes,newSelectList(new[]{new{Value="0",Text="00"},new{Value="15",Text="15"},new{价值="30",Text="30"},new{Value="45",Text="45"},},"Value","Text"))Billable@Html.CheckBoxFor(model=>model.Billable)输入备注:@Html.TextAreaFor(model=>model.Comment)您的视图属于BillingCodeList类型,但您尝试将列表发送到控制器时尝试更改操作,如下所示:[HttpPost]publicvoidSubmitTimesheet(BillingCodeListmodel){字符串uri="";foreach(varbillingCodeinmodel.BillingCodes){//用这些中的每一个做东西}}编辑:在将模型发送到视图List()之前,您似乎没有将BillingCodeList的列表元素初始化为新元素。还是你上面是C#学习教程:在controller的post方法中,MVC4模型中的提交表单是空的。分享的所有内容,如果对大家有用,需要了解更多C#学习教程,希望大家多多关注——本文来自网络收藏,不代表立场,如涉及侵权,请点击有权联系管理员删除。如需转载请注明出处: