I want to do a drop down list and when I click the submit button, it posts back the selected value.
I don’t know if I’m doing it right but I’ve searched around and came up with this so far.
I just want to know if I’m doing it right and where it would store the value once it posts back.
@using (Html.BeginForm())
{
@Html.DisplayName("Name:")<br />
@Html.TextBox("NAME")<br />
@Html.DisplayName("Password:")<br />
@Html.TextBox("PASS")<br />
@Html.DisplayName("Team Name:")<br />
@Html.DropDownListFor(x => x.Teams, new SelectList(Model.Teams, "value", "TeamNAME"), new {onchange = "submit()"})
<input type="submit" value="ADD" />
}
Want to get the value here
[HttpPost]
public ActionResult Index()
{
//GET VALUE OF THE SELECTED TEAM HERE
return RedirectToAction("Index");
}
It looks pretty close — the only thing is that, you should be consistent in using the model-bound HTML helper methods, or not. That is, use
TextBoxForandDropDownListFor, orTextBoxandDropDownList, but don’t mix and match.If you use the model-bound ones, then you should be able to simply add the model type as a parameter to your postback action:
For the unbound ones, you can add parameters individually using their names:
(assuming you’d change to
@Html.DropDownList("TEAM", new SelectList(Model.Teams, "value", "TeamNAME"), new {onchange = "submit()"}))