我的应用程序有一个自定义会员资格,它与通用会员资格几乎相同。在其他细节中,不同之处在于我如何将值传递给我Register
的 post 方法。
到目前为止,我的方法参数中有用户名、密码、名字、...、状态,所有这些都是字符串(还有更多但与问题无关),如下所示:
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; }
}
SelectList
并在我的Register View
方法中添加适当的。
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
空。
我已经尝试了更多的想法,但到目前为止还没有。