我有一个自定义成员资格为我的应用程序,它几乎与通用一样。除了其他细节之外,不同的是我如何将值传递给我的Register
post方法。从文本框更改为DropDownList
直到现在我的用户名,密码,名字,...,状态,都如弦在我的方法的参数(还有更多,但无关的问题),像这样:
public ActionResult Register(string userName, string password, string confirmPassword, string firstName, string lastName, string address, string city, string state, string zip)
问题在手是State
参数,现在我希望它从一个下拉列表中传递,而不是从文本框传递到目前为止。
我制作了一个模型,用于填充下拉菜单。
public class State
{
public int StateID { get; set; }
public string StateName { get; set; }
}
在我Register View
方法添加适当SelectList
。
public ActionResult Register()
{
ViewBag.StateID = new SelectList(db.States, "StateID", "StateName");
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View();
}
然后我改变了Register
View
,并提出了下拉,而不是Html.TextBoxFor
帮手。
@Html.DropDownList("StateID", (SelectList)ViewBag.StateID, new { @class = "ddl" })
需要注意的是,所有这些参数不包括username
和password
,保存在User Profile
性能。这是如何在Register
post方法中完成的。
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();
最后,问题是它没有得到保存。该用户的State
属性为Profile
为空。
我已经尝试了几个更多的想法,但没有到目前为止。
感谢。 +1,因为这也是正确的。 – rexdefuror
应该先写一个答案,但总是有一些额外的解释=) –