So I’ve looked around StackOverflow a lot and found a nice solution to my problem.
MVC3 DropDownListFor – a simple example?, this ought to do it for me. BUT, it returned a null value… Somehow I have no idea how to go around this so assistance will be appreciated.
Model AccountModel.cs
...
public string Address { get; set; }
public string City { get; set; }
public class StateList
{
public int StateID { get; set; }
public string Value { get; set; }
}
public IEnumerable<StateList> StateListOptions = new List<StateList>
{
//new StateList { StateID = -1, Value = "Select State" },
new StateList { StateID = 0, Value = "NY" },
new StateList { StateID = 1, Value = "PO" }
};
public string State { get; set; }
public string Zip { get; set; }
...
Register.cshtml
@Html.DropDownListFor(m => m.State, new SelectList(Model.StateListOptions, "StateID", "Value", Model.StateListOptions.First().StateID))
I thought maybe my StateID = -1 made it output a null for some reason… but it didn’t, you can see it commented out here. What did I do wrong?!
Get Action
public ActionResult Register()
{
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View();
}
Create an object of your Model/ ViewModel and send that to view.
Now your View should be strongly typed to this Model
So in your
Register.cshtmlview,To Get the Selected State in POST, You can check the
StateProperty value.