2012-08-22 30 views
1

我有一个自定义成员资格为我的应用程序,它几乎与通用一样。除了其他细节之外,不同的是我如何将值传递给我的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(); 
} 

然后我改变了RegisterView,并提出了下拉,而不是Html.TextBoxFor帮手。

@Html.DropDownList("StateID", (SelectList)ViewBag.StateID, new { @class = "ddl" }) 

需要注意的是,所有这些参数不包括usernamepassword,保存在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为空。

我已经尝试了几个更多的想法,但没有到目前为止。

回答

2

下拉应该与您希望映射到的参数具有相同的名称。它看起来像是“StateID”,但它应该读取“状态”(作为参数的名称)。

所以应该阅读:已经与马里奥斯帮助解决

@Html.DropDownList("State", (SelectList)ViewBag.StateID, new { @class = "ddl" }) 
1

问题在于,您在操作中尝试将其映射到的参数的下拉列表中使用了不同的名称。

如果你做了两个匹配,那么这应该有助于解决你的问题。

所以,你应该将其更改为:

@Html.DropDownList("State", (SelectList)ViewBag.StateID, new { @class = "ddl" }) 

希望这有助于。

+1

感谢。 +1,因为这也是正确的。 – rexdefuror

+1

应该先写一个答案,但总是有一些额外的解释=) –