C# 表单值未传递给控制器

C# 表单值未传递给控制器,c#,asp.net-mvc,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 value

我的控制器方法:

 [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中的数据类型设置为NOTNULL。为什么会这样

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

查看代码:

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

通过查看,这些值正在被传递。

结果表明,控制器方法的参数需要与将要传递给它的参数拼写相同。例如:

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

这是一个简单的错误,但我花了一些时间来解决这个问题。希望这对其他人有帮助

发布您的表单代码,如果您可以发布新库存表单的Get方法。嗨,Luv2Learn!你介意发表你的观点吗?我已经多次遇到这个问题,因为我的html中的名称标记不正确,无法将ASP.Net的表单数据转换为您的模型。对问题进行了更改,以显示视图代码和获取视图页面的方法。您不应该期望模型的每个属性,而应该期望模型本身。这是非常糟糕的编码,不遵循任何ASP.NET MVCconvention@CamiloTerevinto你能提供一个链接到我可以阅读的地方吗?当然,看,更新创建页面和onwards@CamiloTerevinto谢谢我实际上是在做一个具有基本功能的简单库系统,但你所指出的对实践很重要!