表单值未传递给控制器

编程入门 行业动态 更新时间:2024-10-27 18:20:01
本文介绍了表单值未传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的控制器方法:

[HttpGet] public ActionResult NewInventory() { return View(); } [HttpPost] public async Task<ActionResult> NewInventory(string bookID, string ttlin, string lowin, string Outnow) { // test values passed, etc..... }

到目前为止,仅正确传递了"lowin"值.所有其他值都设置为"0"(我相信由于在SQL DB中将数据类型设置为"not null").为什么会这样?

So far only the "lowin" value is being passed correctly. All the other values are set to "0" (I believe due to the datatypes being set to "not null" in SQL DB). Why is this?

我假设是因为仅正确传递了一个值,并且没有引发任何异常,所以视图页面代码缺少要传递的其他字段.

I assume because only one value is passed correctly and no exceptions are thrown, then the view page code is missing the other the fields to pass.

查看代码:

@model LibraryMS.Inventory @{ ViewBag.Title = "newinventory"; } <h2>newinventory</h2> @using (Html.BeginForm("NewInventory","BookInfo", FormMethod.Post)) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Inventory</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.BookID, "BookID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.BookID, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.BookID, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.TotalIn, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.TotalIn, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.TotalIn, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.LowIn, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.LowIn, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.LowIn, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Out, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Out, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Out, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div>

通过观察,这些值正在传递.

By looking, the values are being passed.

推荐答案

结果证明,控制器方法的参数拼写与传递给它的参数相同.例如:

Turns out that the controller method's parameters needs to be spelled the same as what is going to be passed to it. For example:

使用html帮助程序@Beginform自动生成的表单中,所有字段均存在.但是controller方法中的参数与清单的类字段不同.

With the autogenerated form using html helper @Beginform, all the fields are present. But the parameters in the controller method are not the same as the inventory's class fields.

public partial class Inventory { public string BookID { get; set; } public short TotalIn { get; set; } public short LowIn { get; set; } public short Out { get; set; } public virtual BookInfo BookInfo { get; set; } }

与参数比较:

[HttpPost] public async Task<ActionResult> NewInventory(string bookID, string ttlin, string lowin, string Outnow) { // test values passed, etc..... }

解决方法是使粗略的参数相同,大小写无关紧要.

The fix was to ofcoarse make the params the same, capitalization does not matter.

[HttpPost] public async Task<ActionResult> NewInventory(string bookID, string totalin, string lowin, string Out) { // test values passed, etc..... }

这是一个简单的错误,但花了我一些时间才能解决.希望这可以帮助其他人!

It was a simple mistake but took me some time to figure this out. Hopefully this helped someone else!

更多推荐

表单值未传递给控制器

本文发布于:2023-10-26 10:53:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   控制器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!