2012-08-22 9 views
1

내 앱에 대한 맞춤 멤버십이 있습니다. 일반적인 앱과 거의 같습니다. 무엇보다 다른 점은 내 Register 게시 방법에 값을 전달하는 방법입니다.TextBox에서 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 도우미 대신 드롭 다운을했다. 이 모든 매개 변수 usernamepassword을 제외

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

참고, 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

드롭 다운의 매핑 대상 매개 변수와 이름이 같아야합니다. 그것은 id가 "StateID" 인 것처럼 보이지만, "상태" (매개 변수의 이름으로)을 읽어야합니다.

그래서 읽어야 이미 마리오의 도움으로 해결

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

문제는 액션에 매핑하려고하는 매개 변수의 드롭 다운에 다른 이름을 사용했기 때문입니다.

두 개가 일치하면 문제를 해결하는 데 도움이됩니다. 이 도움이

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

희망 :

그래서 당신은 그것을 변경해야합니다.

+1

감사합니다. +1 또한 정확합니다. – rexdefuror

+1

먼저 답을 써야하지만, 몇 가지 추가 설명이 있으면 좋을 것입니다 =) –