表单值未传递给控制器​​

时间:2017-05-22 17:35:04

标签: c# asp.net-mvc

我的控制器方法:

 [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”)。这是为什么?

我假设因为只有一个值正确传递并且没有抛出异常,所以视图页面代码缺少另一个要传递的字段。

查看代码:

 @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>

通过查看,值正在传递。

1 个答案:

答案 0 :(得分:-1)

事实证明,控制器方法的参数拼写需要与将要传递给它的内容相同。例如:

使用html helper @Beginform的自动生成表单,所有字段都存在。但是控制器方法中的参数与库存的类字段不同。

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.....
}

修复是为了使参数相同,大写并不重要。

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

这是一个简单的错误,但花了我一些时间来弄明白这一点。希望这有助于其他人!