1

我的应用程序有一个自定义会员资格,它与通用会员资格几乎相同。在其他细节中,不同之处在于我如何将值传递给我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属性中。这就是在Registerpost 方法中完成的方式。

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空。

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

4

2 回答 2

2

下拉列表应与您希望它映射到的参数具有相同的名称。看起来它的 id 是"StateID",但它应该是"state"(作为参数的名称)。

所以它应该是:

@Html.DropDownList("State", (SelectList)ViewBag.StateID, new { @class = "ddl" }) 
于 2012-08-22T08:33:33.337 回答
1

问题是您在下拉列表中使用了与您尝试在操作中映射它的参数不同的名称。

如果您使两者匹配,那么这应该有助于解决您的问题。

所以你应该把它改成:

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

希望这可以帮助。

于 2012-08-22T08:27:52.777 回答