Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

c# - Form values not being passed to controller

My Controller Method:

 [HttpGet]
    public  ActionResult NewInventory()
    {
        return View();
    }

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

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.

View Code :

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

Compared to the paramters:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...