Guest User

Guest User

  • Tech Writer
  • 515
  • 47k

About CheckboxList in MVC

Jun 27 2020 1:21 PM
Hello Everyone,
 
Hope everyone is fine !
 
I have bind Contact in CheckBoxList successfully.
 
But i am not ableto save the checked item to database.Everyone while saving , it shows the List as Null.
 
Following is my View Code :
  1. @foreach (var item in ViewBag.ContactsCheckbox)  
  2. {  
  3. <div class="custom-control custom-checkbox">  
  4. <input id="chk@(item.Id)" class="custom-control-input" type="checkbox" value="@item.Id" checked="@item.IsChecked" />  
  5. <label class="custom-control-label" for="chk@(item.Id)">@item.ContactName </label>  
  6. </div>  
  7. }  
Following is Controller Code :
  1. public ActionResult Add(MemoriesImagesMaster mi, List<ContactsMaster> items)  
  2. {  
  3. Do_Validation val = new Do_Validation();  
  4. List<MemoriesImagesMaster> list = new List<MemoriesImagesMaster>();  
  5. Guid LoginId = new Guid(Session["UserId"].ToString());  
  6. foreach (ContactsMaster item in items)  
  7. {  
  8. if (item.IsChecked)  
  9. {  
  10. ViewBag.Message += string.Format("{0}\\n", item.ContactName);  
  11. }  
  12. }  
  13. }  
On View, the data is bind using Memory Model and on same the list is getting bound using the Contact Model
 
Thank you in advance.

Answers (3)