0

MVC3 인터넷 응용 프로그램이 있습니다.각 라디오 버튼의 고유 이름을 유지하는 방법

기본적으로 등록 양식이 있으며, 여기에는 3 개의 라디오 버튼이 있습니다. 라디오 버튼 중 하나를 선택하고 submit 버튼을 누를 때마다 컨트롤러로 가서 내 데이터베이스에 데이터를 삽입해야합니다.

또한 Registration 양식의 입력란에 대한 유효성 확인이 있습니다. stackoverflow에서

어딘가에는 내가 Property names in the Model and the Names of the fields in the view have to be matched 중위에도 유효성 검사 오류 후 형태로 값을 유지하기 위해 읽었습니다.

이 문은 텍스트 상자 및 드롭 다운 목록에 완벽하게 작동합니다.

내 질문 : 어떻게 foreach 고유 이름을 가지고 radiobutton?

내 양식에는 3 개의 라디오 버튼이 있습니다.

말은,이 내가

public string Phone1 { get; set; } 
public string Phone2 { get; set; } 
public string Phone3 { get; set; } 
//also i have a preferred phone--which sets if any of the phone is selected 
public bool PreferredPhone { get; set; } 

나는이

Phone1: 
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone1, new { id = "Phone1", @onclick = "PrefferedPhone(this)" }) 
Phone2: 
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone2, new { id = "Phone2", @onclick = "PrefferedPhone(this)" }) 
Phone3: 
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" }) 

그러나이 같은 것을 해봤 한 라디오 버튼으로 내 모델에서

Phone 1 // here is the radio button1 
Phone 2 // here is the radio button2 
Phone 3 // here is the radio button3 

을 다음에 텍스트 상자입니다 선언은 모든 라디오 버튼의 이름을 PreferredPhone

으로 설정합니다.

각 라디오 버튼에 다른 이름을 지정하는 방법은 무엇입니까?

심지어 RadioButton 이름을 model=>model.Phone1, model=>model.Phone2, model=>model.Phone3으로 유지하려고 시도했지만, 이렇게 설정하면 세 개의 라디오 버튼을 모두 선택할 수 있습니다.

답변

0

세 개의 라디오 버튼 모두를 선택하면 대화 상대가 체크 박스 인 것처럼 말하는 것처럼 들립니다. 동일한 그룹에서 고려되는 라디오 버튼을 동시에 선택할 수는 없지만 그룹당 하나만 선택합니다 (하지만 이미 알고 있고 오해입니다).

모델의 속성 이름과보기의 필드 이름이 일치해야하는 Stackoverflow의 어딘가에서 읽은 것부터.

@Html.RadioButton("stuffRadio", 1) 
@Html.RadioButton("stuffRadio", 2) 
@Html.RadioButton("stuffRadio", 3) 
@Html.RadioButton("stuffRadio", 4) 

그리고 양식이 게시 된 컨트롤러 : 미안 다음 misunderstang 경우

[HttpPost] 
public ActionResult TheAction(Model model, string stuffRadio) 
{ 
    //dostuff to model and other things 
    string value = stuffRadio; 
    //dostuff to the selected radioStuff :) 
} 

어쨌든, 내가 물어보고 싶은 포어 경우이 같은 뷰에서 라디오 버튼이있는 경우 너는 대가로 너. 당신은 각 라디오 버튼을 구별하고 싶다고 말합니다. 왜 그런지 모르겠습니다. 사후 조치와 컨트롤러에 모든 전화 번호를받을 수 있습니까?나는 물론 것 모든 도스 그룹 선택을 읽고 싶은 경우에 매개 변수를 사용하여 같은 일을 다시

@Html.RadioButton("stuffRadioGroup1", 1) 
@Html.RadioButton("stuffRadioGroup2", 2) 
@Html.RadioButton("stuffRadioGroup3", 3) 
@Html.RadioButton("stuffRadioGroup4", 4) 

그러나 한 :

단순히 같은 것을 할 것입니다 내 자신의 예를 사용하여 이 같은 컨트롤러 :

다시
[HttpPost] 
public ActionResult TheAction(Model model, 
    string stuffRadioGroup1, string stuffRadioGroup2, 
    string stuffRadioGroup3, string stuffRadioGroup4) 
{ 
    //dostuff to model and other things 
    string value1 = stuffRadioGroup1; 
    string value2 = stuffRadioGroup2; 
    string value3 = stuffRadioGroup3; 
    string value4 = stuffRadioGroup4; 
    //dostuff to the selected radioStuff :) 
} 

, misunderstang을 했는데요 다음 물론 당신이 저를 정정 할 수있다 나중에) 를!

########## 당신에게 숨겨진 필드를 원한다면, 모든

먼저 ################### 수정 됨 폼에 있어야하며 특정 숨겨진 필드의 이름을 같은 이름의 컨트롤러에 매개 변수로 전달해야합니다. 예를 들면 : 당신이, 내가 거기에 FormCollection를 넣어 볼 수 있듯이

[HttpPost] 
public ActionResult SomeAction(Model mymodel, string derp, FormCollection collection) 
{ 
    int number = Convert.toInt32(derp); 
    //now number should be '1234' 

    string phone3 = collection.Get("Phone3"); 
    //now you have the phone number and the hidden field telling you if that number is preffered 

    TryUpdateModel(mymodel); //populate the model if it hasn´t already been populated 

    if(ModelState.IsValid) 
    { 
     //do stuff 
    else 
    { 
     //TODO: here you should set the model with the stuff from derp and collection before returning 
     return View("SameViewAsBefore", mymodel); 
    } 
} 

것은 단지 가지고 :

@(Html.BeginForm("SomeAction", "Controller") 
{ 
    <input type="hidden" name="derp" value="1234" /> 
    @Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" }) 
    <button type="submit">submit</button> 
} 

숨겨진 필드가 아니라 통과 될 것으로 예상되는 경우 컨트롤러는 다음과 같이해야한다 다른 접근 방식. 희망이 있으면 도움이 되겠지만, 항상 다시 물어볼 수 있습니다. (때때로 가끔 오해하는 것으로 알려져 있습니다)

+1

기본적으로 내가 필요한 것은 선택된 라디오 버튼의 값을 유지하는 것입니다. 내가 자바 스크립트를 사용하여 노력했지만, 숨겨진 필드에 선택한 값을 저장하는 자바 스크립트에서 함수를 만들었지 만 그 doesnot 작동합니다. 그래서, 만약 내가 라디오 btns에 대한 별개의 이름을 가지고, 나는 '보기'에서'컨트롤러'로 전달할 수있는 값을 가질 수 있습니다. 의견이 있으시면 –

+0

안녕하세요 @ Karthik 제가 내 대답을 편집했습니다. 아마도 도움이 될까요? :) – gardarvalur

+0

고마워,하지만 그게 내가 선호하는 전화 값을'querystring' 권리로주는거야? 왜 다른 모든 양식 필드가 캡처되고 라디오 버튼이 캡처되지 않는 이유는 무엇입니까? –

관련 문제