I have a custom Membership for my app, and it’s pretty much the same as the generic one. Among other details what’s different is how I pass values to my Register post method.
Until now I had username, password, firstName, …, state, all as strings (there’s more but irrelevant for the question) in my method parameters, like so:
public ActionResult Register(string userName, string password, string confirmPassword, string firstName, string lastName, string address, string city, string state, string zip)
The issue at hand is State parameter, now I want it to be passed from a dropdown not from a textbox as it was so far.
I’ve made a model for dropdown to be populated.
public class State
{
public int StateID { get; set; }
public string StateName { get; set; }
}
And added appropriate SelectList in my Register View method.
public ActionResult Register()
{
ViewBag.StateID = new SelectList(db.States, "StateID", "StateName");
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View();
}
Then I’ve altered the Register View, and made a dropdown instead of Html.TextBoxFor helper.
@Html.DropDownList("StateID", (SelectList)ViewBag.StateID, new { @class = "ddl" })
Note that, all these parameters excluding username and password, are saved in User Profile properties. This is how that’s done in Register post method.
ProfileBase _userProfile = ProfileBase.Create(userName);
_userProfile.SetPropertyValue("FirstName", firstName);
_userProfile.SetPropertyValue("LastName", lastName);
_userProfile.SetPropertyValue("Address", address);
_userProfile.SetPropertyValue("City", city);
_userProfile.SetPropertyValue("State", state);
_userProfile.SetPropertyValue("Zip", zip);
_userProfile.Save();
Lastly, the problem is that it doesn’t get saved. The State property for that users Profile is empty.
I’ve tried several more ideas but nothing so far.
The dropdown should have the same name as the parameter you want it to map to. It looks like it’s id is “StateID”, but it should read “state” (as the name of the parameter).
So it should read: