sushiBite
sushiBite

Reputation: 361

Razor @Html.DropDownListFor submit & get the option value on the server

I am trying to submit when a dropdownlist changes by doing this

@using (Html.BeginForm("testAction", "FishingTrip"))
{
    @Html.DropDownListFor(x => x.Day, Model.Days,  new { onchange="this.form.submit();" })
}

This works fine but I am having problems (in other words don't know how) to get the option value on the server, can anybody help me with this ?

cheers sushiBite

Upvotes: 1

Views: 4695

Answers (2)

SLaks
SLaks

Reputation: 888205

Just use the Day property of the model parameter, like any other property / editor.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039488

You could simply have your POST controller action take it as parameter and leave the default model binder to the binding:

[HttpPost]
public ActionResult TestAction(string day)
{
    // The day parameter will contain the selected value
    ...
}

Or directly use the view model you used in the view:

[HttpPost]
public ActionResult TestAction(MyViewModel model)
{
    // The model.Day parameter will contain the selected value
    ...
}

Upvotes: 1

Related Questions