Reputation: 2200
I am trying to pass a value from a dropdownlist to a controller method as a parameter. The following code DOES NOT WORK:
@using (Html.BeginForm())
{
<fieldset>
<legend>Submit</legend>
<div class="editor-field">
@Html.DropDownListFor(m => m.ClientId, new SelectList(Model.Clients, "ClientId", "Name"))
@Html.ValidationMessageFor(model => model.ClientId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
This is my controller method in brief:
[HttpPost]
public ActionResult SaveInvoice(string ClientId)
{
//some code here
return RedirectToAction("SaveInvoice/Complete", new { id = invoice.InvoiceId });
}
When I click submit on the form, the controller method does not fire. No creation of invoice in the database, no redirect to the "Complete". I am quite a noob at this, so if you have the time please explain carefully what I need to do. It is very important that this controller fires and then REDIRECTS. Thank you all.
Upvotes: 0
Views: 650
Reputation: 28747
The problem is probably that you are not specifying the correct controller in your Html.BeginForm. Try adding the action and the controller explicitly. (I don't know the name of your controller so I invented something, please fill in the correct value)
@using (Html.BeginForm("SaveInvoice", "yourControllerName"))
{
<fieldset>
<legend>Submit</legend>
<div class="editor-field">
@Html.DropDownListFor(m => m.ClientId, new SelectList(Model.Clients, "ClientId", "Name"))
@Html.ValidationMessageFor(model => model.ClientId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Another possibility would be that your validation is stopping the form from being submitted. Check that your data is valid (or temporarily remove the validator)
Upvotes: 2
Reputation: 16595
This is assuming all your routing is setup correctly. With a parameterless Html.BeginForm()
on the view, you're posting to the same action that you loaded the with. I'm guessing you SaveInvoice
action isn't an overload of the same action you loaded the page with.
Try overloading the action you are using to load the HTML form, or use something like Html.BeginForm("SaveInvoice", "{controller name here}")
.
Upvotes: 1