为什么点击提交按钮后,用户代码和员工 ID 的转换失败?

问题描述 投票:0回答:1

我从事 asp.net MVC Web 应用程序。我遇到问题

RequesterIndex
Post 类型的操作无法将用户代码转换为员工 ID。

行动

Requester index
施法失败时使用火后类型

它给我消息无效的文件号。

为什么我会显示此消息。我为会话用户代码分配值

对于员工 ID,我还将员工 ID 存储在隐藏字段中以避免重置

单击提交按钮后的值。

为什么点击提交按钮时没有显示消息无效文件这是我的

问题?

1 - 代表员工 ID 的模型属性

EmpId

public class ResignationRequester { public int EmpID { get; set; } }
2 - 控制器,具有加载数据时获取请求者索引的操作结果

http://ucuatappweb:907/Resignation/RequesterIndex?filenumber=123650 public class ResignationController : Controller { [HttpGet] public ActionResult RequesterIndex(string filenumber) { int employeeFileNo; if (!string.IsNullOrEmpty(filenumber)) { if (int.TryParse(filenumber, out employeeFileNo)) { Session[SessionKeys.UserCode] = employeeFileNo; resignationRequester.EmpID = Convert.ToInt32(Session[SessionKeys.UserCode].ToString()); Session[SessionKeys.UserCode] = employeeFileNo; } } else { ViewBag.errorMsg = "unauthorized user"; } return View(resignationRequester); }
3 - 当单击“提交”按钮时,请求者索引的类型为“火后”

[HttpPost] public JsonResult RequesterIndex(ResignationRequester resignationRequester) { dynamic responseData = new ExpandoObject(); responseData.success = false; responseData.message = ""; try { var filenumber = resignationRequester.EmpID; if (Session[SessionKeys.UserCode] != null) { if (int.TryParse(Session[SessionKeys.UserCode].ToString(), out int empId)) { resignationRequester.EmpID = empId; } else { responseData.success = false; responseData.message = "Invalid File No"; return Json(responseData); } } else { responseData.success = false; responseData.message = "No Data For This File No"; return Json(responseData); } } catch (System.FormatException ex) { responseData.success = false; responseData.message = ex.Message; } return Json(responseData); } }
4 - 查看显示数据 

requesterIndex.cshtml


<h2 style="font-weight: bold; padding-bottom: 5px;padding-top:50px;">Resignation Form</h2> <form id="ResignationApp" style="padding-top: 50px; border: 2px solid black;padding-left:20px;font-size:16px;"> @Html.AntiForgeryToken() <div class="row"> <div class="form-group col-md-6"> <div class="col-md-5"> @Html.LabelFor(model => model.EmpID, htmlAttributes: new { @class = "control-label" }) </div> <div class="col-md-7"> @Html.EditorFor(model => model.EmpID, new { htmlAttributes = new { @class = "form-control", id = "txtempid", @readonly = "readonly" } }) @Html.HiddenFor(model => model.EmpID, new { htmlAttributes = new { @class = "form-control", id = "hiddentxtempid", @readonly = "readonly" } }) </div> </div> </div> <div class="col-md-7"> <input id="btnsubmit" value="Submit" type="submit" style="background-color: #05014a;font-size:18px;margin-top:20px; color: #fff; height: 40px; width: 200px;margin-bottom:40px;" /> </div> </form> <script> $(document).ready(function () { var value = $("#txtempid").val(); $("#hiddentxtempid").val(value); $('#btnsubmit').click(function () { var empidval = $("#txtempid").val(); var formData = $("#ResignationApp").serialize(); $.ajax({ type: "POST", dataType: 'json', url: '@Url.Action("RequesterIndex", "Resignation")', data: formData, success: function (response) { }, error: function (xhr, status, error) { } }); }); $("#ResignationApp").submit(function (e) { e.preventDefault(); }); }); </script>

更新帖子

在 POST 操作函数中,如何使用隐藏字段中的值? 你能通过代码澄清一下吗

如何依赖 jQuery 上的隐藏字段和操作并通过代码答案查看请求者索引和操作结果

javascript jquery asp.net asp.net-mvc c#-4.0
1个回答
0
投票
根据您最近的编辑,我会这样做:

  1. 摆脱

    @Html.EditorFor(model => model.EmpID, new { htmlAttributes = new { @class = "form-control", id = "txtempid", @readonly = "readonly" } })

    ,只依赖隐藏字段

  2. 在您的发布端点中,使用以下方式获取值:resignationRequester.EmpID - 这是基于在 GET 端点中正确设置 resignationRequester.EmpID 的假设。

© www.soinside.com 2019 - 2024. All rights reserved.