None
None

Reputation: 9247

How to pass dropdown list value from view to controller

I have this in controller

public ActionResult Register()
{
    ViewBag.CurrencyId = new SelectList(CommonDbService.GetAllCurrency(),"Id","Name");
    return View();
}

I have class where i populate this dropdown and show it in view :

public static Currency[] GetAllCurrency()
{
    Currency [] currency  = Currency.GetAllCurrencies();
    return currency;
}

This is my view :

@Html.LabelFor(m => m.Currency)
@Html.DropDownList("CurrencyId","Select currency...")

I need to get value from dropdown list in view and pass it to controller :

Model.Currency.Find(dropdown list value); <--- here

Upvotes: 1

Views: 1520

Answers (1)

Craig W.
Craig W.

Reputation: 18155

Are you passing it to the controller on POST? If so, make sure your post method has a model defined as an input value, make the view strongly-typed using that model, and use @Html.DropDownListFor. The selected value will automatically be put in the model and passed back on POST.

Upvotes: 1

Related Questions