2

I have a form:

@using (Html.BeginForm("QuoteUpdate", "Home", FormMethod.Post))
{

@Html.DropDownList("network", Model.availableNetworks);
@Html.DropDownList("grade", Model.grades);
@Html.HiddenFor(o => o.Product.id);

<button type="submit">Get Quote</button>
}

And a controller:

    [HttpPost]
    public ActionResult QuoteUpdate(int? id, string network, string grade )
    {
    }

The id property is always null after form is submitted. I have checked source and the hidden field has the correct value in the rendered HTML.

I cannot figure out why this parameter is always null. What am I missing?

Guerrilla
  • 13,375
  • 31
  • 109
  • 210

1 Answers1

10

Since you're accessing a nested property on your model, the generated HTML is probably something like this:

<input type="hidden" id="Product_id" name="Product.id" />

When your form gets posted to the controller action, there's not a parameter that matches up with Product.Id.

You could work around this by changing the generated name of the input (see this answer for more about how to do that):

@Html.HiddenFor(o => o.Product.id, new { Name = "id" });

Which will generate:

<input type="hidden" id="Product_id" name="id" />

Then things should model bind correctly.

Community
  • 1
  • 1
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307