Tôi đang phát triển một ứng dụng Web ASP.Net MVC 3 và tôi đang gặp một số khó khăn khi nhận các giá trị từ danh sách kiểm tra. Tôi đã đọc hầu hết các câu hỏi trên Stackoverflow xung quanh khu vực này, tuy nhiên, tôi vẫn còn một số vấn đề.ASP.Net MVC 3 Lấy giá trị danh sách hộp kiểm
Tôi có một ViewModel
public class ViewModelCheckBox
{
public string Id { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
}
Một ViewModel mà sử dụng viewmodel trên
public class ViewModelAssignSubSpeciality
{
public ListItem Item { get; set; }
public IList<ViewModelCheckBox> SpecialityList { get; set; }
}
Sau đó, trong điều khiển của tôi
public ActionResult AssignSubSpeciality(int id)
{
//Get a list of all sub-specialities
var SpecialityList = _listService.GetListItemsByID(3).ToList();
//Get a list of sub-specialities for the the passed in id, this is either the id of a speciality or grade
IList<RelationshipSpecialitySub> assignedSpecialities = _listService.GetAssignedSubSpecialities(id).ToList();
var checkBoxList = new List<ViewModelCheckBox>();
foreach (ListItem item in SpecialityList)
{
ViewModelCheckBox chkBox = new ViewModelCheckBox { Id = item.listItemID.ToString(), Name = item.description };
//If sub-speciality exists in assignedSpecialities list, then make checkbox checked
foreach (var specilaity in assignedSpecialities)
{
if (specilaity.subID == item.listItemID)
{
chkBox.Checked = true;
}
else
{
chkBox.Checked = false;
}
}
checkBoxList.Add(chkBox);
}
ViewModelAssignSubSpeciality viewModel = new ViewModelAssignSubSpeciality();
viewModel.ListItem = _listService.GetListItemByID(id);
viewModel.SpecialityList = checkBoxList;
return View(viewModel);
}
Các mã trong bộ điều khiển trên là nhận được một danh sách của tất cả các mục danh sách kiểm tra có thể có, sau đó nhận danh sách tất cả các c đã chọn trước đó các mục danh sách heckbox mà nó đặt tùy chọn đã chọn thành true.
Chế độ xem của tôi sau đó sẽ trông như thế này, lặp qua SpecialityList và tạo hộp kiểm cho từng mục và cũng đặt giá trị được chọn thành true nếu cần.
<fieldset>
<legend>Specialities</legend>
@foreach (var item in Model.SpecialityList)
{
<input type="checkbox" id="@item.Id" name="@item.Name" value="@item.Id" @(item.Checked ? "checked" : "") />
<label for="@item.Id">@item.Name</label><br />
}
<input type="submit" value="Save Changes" class="sepH_b" />
My HttpPost phương pháp trong điều khiển của tôi sau đó trông như thế này
public ActionResult AssignSubSpeciality(ViewModelAssignSubSpeciality model)
{
//delete all sub-specialities in tbl relationshipSpecialitySub for List
foreach (ViewModelCheckBox item in model.SpecialityList)
{
//_listService.DeleteSubSpecialityFromSpeciality(item.Id);
}
return RedirectToAction("ListItems", new { id = model.ListItem.listID });
}
Tuy nhiên, khi tôi cố gắng để có được các hộp kiểm được chọn trong
model.SpecialityList
Nó chúng tôi luôn luôn là null . Tôi không chắc chắn lý do tại sao nó không chứa một danh sách các ViewModelCheckBox.
Có ai có thể giúp tôi với điều này không?
Cảm ơn bạn.
+1 cho mô hình xem và mẫu trình chỉnh sửa. –
Câu trả lời hay. Cảm ơn bạn. – tgriffiths
Thú vị, tôi đã tìm kiếm một cái gì đó giống như thế này trong một thời gian. Cảm ơn. –