ASP.NET网站Software页表单提交失效无法跳转PostSoftware问题求助
问题根因
表单点提交没反应根本不需要额外写JS处理,核心是几个基础写法错误:
- 最致命的问题:你所有输入框、提交按钮都没套
<form>标签,浏览器根本不知道提交哪些数据、往哪提交,点按钮自然没有任何提交动作。 - 控制器方法没区分请求类型:
Software方法没加[HttpGet]/[HttpPost]标记,页面首次加载和表单提交都走同一个方法,提交时如果模型绑定没拿到值,直接就返回空表单视图,根本走不到你写的跳转逻辑。 - 多选下拉框绑定写错了:
MultiSelectList的构造参数传错,就算表单提交了,选中的软件列表也绑定不到模型上。 - 跳转传值逻辑有问题:
RedirectToAction是发起GET请求,没法直接把整个TicketModel复杂对象传到PostSoftware方法,就算前面逻辑都对,跳过去之后模型值也会丢。
修复步骤
1. 修正前端视图,添加form标签
把所有输入控件、提交按钮都包在form标签内,指定提交方式为POST,修正后的前端代码如下:
<div class="jumbotron" style="background-color:#D1D3D4;"> <img src="/Images/TECH_BARlogoBLACK.png" style="width:1000px;height:200px;" class="animate__heartBeat"> <p class="lead" style="background-color: #FFFF00;">Software Request</p> <br> <!-- 新增form标签,注意如果你的控制器不是HomeController,把第二个参数换成实际控制器名 --> @using (Html.BeginForm("Software", "Home", FormMethod.Post)) { <p class="text-left"><b>Enter Computer Name:</b></p> @Html.TextBoxFor(x => x.computerName, new { id = "computerName", @class = "form-control" }) <p style="font-size: 14px; text-align: left;"><b>To obtain your Computer Name: </b>Click Start (bottom left windows icon) and type in System Information, On the 5th line, your Computer Name is listed as the System Name <br></p> <p class="text-left" style="width: 1000px;"><b>Business Justification:</b> <br> @Html.TextAreaFor(x => x.businessJustification, new { id = "businessJustification", @cols = 40, @rows = 3,@style="width: 1000px;", @class = "form-control"}) </p> <label for="Software">Select the Software(s) you need!</label> <br> <!-- 修正多选下拉列表绑定,用ListBoxFor原生渲染多选控件 --> @Html.ListBoxFor(model => model.selectedSoftwareList, new MultiSelectList(Model.softwareList, "Value", "Text"), new { id = "_SelectedSoftwareList", @class = "form-control multiselect-dropdown", style = "width:200px;height:300px;", data_placeholder = "Select Software" }) <br> <br> <button type="submit" class="btn btn-primary btn-lg" style="font-size: 20px">Submit</button> } </div>
注意:不需要手动给控件加name属性,HtmlHelper会自动生成和模型匹配的name值,保证模型绑定正常。原来的
DropDownListFor加multiple属性虽然也能实现多选,但ListBoxFor是专门为多选列表设计的,不需要手动加multiple属性,绑定逻辑更稳定。MultiSelectList第二个参数是选项值字段、第三个是选项文本字段,你原来传的参数顺序是错的,会导致提交值异常。
2. 修正控制器代码,区分GET/POST请求
不要用RedirectToAction传递复杂模型,直接在POST方法里处理提交逻辑,避免传值丢失,修正后的控制器代码:
// 标记为GET请求,用于页面首次加载 [HttpGet] public ActionResult Software() { ViewBag.Message = "Request software through the Tech Bar."; var model = new TicketModel(); var snow = new clsServNowAuth(); var tbl = clsServNowAuth.SoftwareTable(); var tbl2 = clsServNowAuth.SoftwareTableID(); tbl.Wait(); tbl2.Wait(); model.softwareList = tbl.Result; model.softwareListID = tbl2.Result; return View(model); } // 标记为POST请求,接收表单提交数据 [HttpPost] public ActionResult Software(TicketModel model) { // 回发时重新加载下拉选项数据,避免选项丢失 var tbl = clsServNowAuth.SoftwareTable(); var tbl2 = clsServNowAuth.SoftwareTableID(); tbl.Wait(); tbl2.Wait(); model.softwareList = tbl.Result; model.softwareListID = tbl2.Result; // 校验必填字段 if (String.IsNullOrEmpty(model.computerName) || String.IsNullOrEmpty(model.businessJustification)) { ModelState.AddModelError("", "请填写计算机名和业务申请理由"); return View(model); } // 直接处理提交逻辑,不需要额外跳转到PostSoftware List<string> lstUserInfo = clsADInterface.GetInfo(model); var selectedSoftware = model.selectedSoftwareList; clsServNowAuth.SoftwareRequest(lstUserInfo, model.computerName, model.businessJustification, model.softwareList, model.softwareListID); // 提交成功后重定向到成功页,避免用户刷新重复提交 return RedirectToAction("SubmitSuccess"); }
如果一定要保留独立的
PostSoftware方法,只需要把Html.BeginForm的第一个参数改成"PostSoftware",让表单直接提交到这个方法即可,不要用RedirectToAction传模型。
额外说明
- 不需要给提交按钮加任何JS点击处理逻辑,原生form提交就可以正常工作,乱加JS反而可能阻止默认提交行为。
- 如果你用了Bootstrap multiselect之类的第三方下拉插件,确保插件初始化时不会修改select标签的name属性,否则会导致多选值提交失败。
- 提交成功后一定要用重定向跳转到独立的结果页,不要直接返回视图,否则用户刷新页面会重复提交表单。
内容的提问来源于stack exchange,提问作者MystifiedSky
相关产品推荐
相关产品推荐

