मैं एक ASP.Net MVC 3 वेब अनुप्रयोग विकसित कर रहा हूँ और मैं एक checkboxlist से मूल्यों को हो रही के साथ कुछ कठिनाइयों हो रहा है। मैंने इस क्षेत्र के आस-पास स्टैकओवरफ्लो पर पहले से ही अधिकांश प्रश्न पढ़े हैं, हालांकि, मुझे अभी भी कुछ समस्याएं हैं।ASP.Net MVC 3 पुन: प्राप्त करें चेकबॉक्स सूची मान
मैं एक ViewModel है
public class ViewModelCheckBox
{
public string Id { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
}
एक और ViewModel जो मेरे नियंत्रक में ऊपर
public class ViewModelAssignSubSpeciality
{
public ListItem Item { get; set; }
public IList<ViewModelCheckBox> SpecialityList { get; set; }
}
फिर viewmodel का उपयोग
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);
}
नियंत्रक के कोड से ऊपर एक सूची हो रही है सभी संभावित चेकबॉक्स सूची आइटमों में से, सभी पहले चयनित सी की सूची प्राप्त करना हेकबॉक्स सूची आइटम जिसके लिए यह चेक किए गए विकल्प को सत्य पर सेट करता है।
मेरे देखें तो इस तरह दिखता है, SpecialityList से अधिक पाशन और प्रत्येक आइटम के लिए एक चेकबॉक्स बनाने, और भी सच करने के लिए अपने चुने हुए मान सेट करता है, तो जरूरत है हो सकता है।
<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" />
मेरे HttpPost मेरी नियंत्रक में विधि तो, इस
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 });
}
हालांकि तरह लग रहा है जब मैं
model.SpecialityList
यह हमें हमेशा अशक्त में चेकबॉक्स चयनित प्राप्त करने की कोशिश । मुझे यकीन नहीं है कि इसमें ViewModelCheckBox की सूची क्यों नहीं है।
किसी कृपया मुझे इस के साथ मदद कर सकते हैं?
धन्यवाद।
+1 मॉडल और संपादक टेम्पलेट्स देखने के लिए +1। –
ग्रेट उत्तर। धन्यवाद। – tgriffiths
दिलचस्प, मैं कुछ समय के लिए ऐसा कुछ ढूंढ रहा हूं। धन्यवाद। –