表单值未传递给控制器 [英] Form values not being passed to controller

查看:48
本文介绍了表单值未传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器方法:

 [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!

这篇关于表单值未传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆