언제나 의해 시작으로 목록 상자
카테고리 : 여기
폼의 샘플입니다 뷰 모델 정의 :
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" />
<% } %>
감사 대린, 는 그 값이 DB에서 올 것 같은 체크 박스의 동적 목록을 사용할 수 있습니까? 환호 – 303
확실히 'IEnumerable'을 사용하십시오. –