2010-07-06 1 views
1

여러 개의 itmes가 선택된 목록 상자와 확인란 그룹을 제출하면 목록이 표시되지 않습니다.여러 개의 드롭 다운 및 체크 박스 그룹이있는 ASP.NET MVC 양식

WebForm 프로젝트 인 경우 저에게 문제가되지 않습니다.

체크 박스 그룹과 선택한 항목이 여러 개있는 목록 상자가 포함 된 ASP.NET MVC2의 양식 제출을 보여주는 모범 사례 및 일부 코드 샘플은 무엇입니까? 체크 박스

주제 그룹 - : - 여러 속성 (여러 = "복수")

답변

1

언제나 의해 시작으로 목록 상자

카테고리 : 여기

폼의 샘플입니다 뷰 모델 정의 :

public class MyModel 
{ 
    public bool Check1 { get; set; } 
    public bool Check2 { get; set; } 

    public IEnumerable<SelectListItem> ListItems { get; set; } 
    public string[] SelectedItems { get; set; } 
} 

다음 컨트롤러 :

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new MyModel 
     { 
      Check1 = false, 
      Check2 = true, 
      ListItems = new SelectList(new[] 
      { 
       new { Id = 1, Name = "item 1" }, 
       new { Id = 2, Name = "item 2" }, 
       new { Id = 3, Name = "item 3" }, 
      }, "Id", "Name") 
     }; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(MyModel model) 
    { 
     // TODO: Process the model 
     // model.SelectedItems will contain a list of ids of the selected items 
     return RedirectToAction("index"); 
    } 
} 

마지막으로보기 :

<% using (Html.BeginForm()) { %> 

    <div> 
     <%: Html.LabelFor(x => x.Check1) %> 
     <%: Html.CheckBoxFor(x => x.Check1) %> 
    </div> 

    <div> 
     <%: Html.LabelFor(x => x.Check2) %> 
     <%: Html.CheckBoxFor(x => x.Check2) %> 
    </div> 

    <div> 
     <%: Html.ListBoxFor(x => x.SelectedItems, Model.ListItems) %> 
    </div> 

    <input type="submit" value="OK" /> 

<% } %> 
+0

감사 대린, 는 그 값이 DB에서 올 것 같은 체크 박스의 동적 목록을 사용할 수 있습니까? 환호 – 303

+0

확실히 'IEnumerable '을 사용하십시오. –

관련 문제