2012-03-02 2 views
0

나는 체크 박스를 가지고 있지만 submited되지 않는 쳤다 값을 제출되는 형태로 ...MVC 확인란 다시 comeing 널

HTML :

@foreach (var radiobutton in Model.InterestedIn) 
      { 
      <span > @Html.CheckBox("selected", radiobutton) 
       <label>@radiobutton</label></span> 
       <br /> 
      } 

모델 :

[Display(Name = "Would you be interested in receiving *")] 
     public IList<string> InterestedIn { get; set; } 

컨트롤러 :

IList<string> lists = new List<string>(); 
      lists.Insert(0, "Latest News"); 
      lists.Insert(1, "Special Offers"); 
      lists.Insert(1, "New Products"); 
      model.InterestedIn = lists; 

게시 방법 :

[HttpPost] 
     public ActionResult Index(Competition model) 
     { 
      if (ModelState.IsValid) 
      { 
+0

컨트롤러의 post 메서드는 어떻게 생겼습니까? 그 서명은 뭐니? 체크 박스 값에 어떻게 접근하려고합니까? –

답변

0

코드가 전혀 컴파일되지 않습니다. CheckBox 도우미는 문자열을 전달하는 동안 두 번째 인수로 부울을 예상합니다.

는 다음과 같이하십시오 : 당신은을 사용하려면

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     IList<string> lists = new List<string>(); 
     lists.Insert(0, "Latest News"); 
     lists.Insert(1, "Special Offers"); 
     lists.Insert(1, "New Products"); 
     var model = new MyViewModel(); 
     model.InterestedIn = lists; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(MyViewModel model) 
    { 
     return View(model); 
    } 
} 

:

public class MyViewModel 
{ 
    [Display(Name = "Would you be interested in receiving *")] 
    public IList<string> InterestedIn { get; set; } 
} 

다음과 같은 컨트롤러 :

@model MyViewModel 

@using (Html.BeginForm()) 
{ 
    foreach (var value in Model.InterestedIn) 
    { 
     <span> 
      <input type="checkbox" name="interestedin" value="@Html.AttributeEncode(value)" /> 
      <label>@value</label> 
     </span> 
     <br /> 
    } 
    <button type="submit">OK</button> 
} 

이 다음 뷰 모델을 가지고 있다고 가정 CheckBox 또는 더 나은 CheckBoxFor 도우미해야합니다. IList<string> 속성이 아니라 IList<CheckBoxItemViewModel> 속성이있는 뷰 모델을 적용하십시오. 여기서 CheckBoxItemViewModel은 레이블을 포함하는 다른 뷰 모델이고이 값의 선택 여부를 나타내는 부울 속성입니다.

+0

내 드롭 다운 목록에 같은 문제가 발생하면 도움이 필요합니까? – Beginner