0

I have a working code of checkbox and it is saving properly in the database, but suddenly they want to have a remark/textbox for each checkbox, i was able to populate textbox with checkbox but it is not saving in my database. how can i do it?

here is my model: ServiceRequestForm and checklist is M:M

 public ServiceRequestForm()
    {
        this.checkLists = new HashSet<checkList>();
    }


 public class checkList
    {
    public checkList()
    {
        this.ServiceRequestForms = new HashSet<ServiceRequestForm>();
    }
    public int checkListId { get; set; }
    public string checkList1 { get; set; }
    public virtual ICollection<ServiceRequestForm> ServiceRequestForms { get; set; }
    }

 ** VIEW MODEL// NOT MAPPED IN THE DATABASE**
 public class srfChecklistVM
   {
    [Key]
    public int checkListId { get; set; }
    public string checkList1 { get; set; }
    public bool Assigned { get; set; }
    public string remarks { get; set; }
   }

What i want is to have a textbox for every checkbox and save it successfully in the database.

my Controller:

 public ActionResult Create(ServiceRequestForm serviceRequestForm, string[] selectedChecklist)
    {
        if (selectedChecklist != null)
        {
            serviceRequestForm.checkLists = new List<checkList>();
            foreach (var check in selectedChecklist)
            {
                var checkToAdd = db.checkLists.Find(int.Parse(check));
                serviceRequestForm.checkLists.Add(checkToAdd);
            }
        }
            db.ServiceRequestForms.Add(serviceRequestForm);
            db.SaveChanges();
            PopulateAssignedData(serviceRequestForm);
     }

     //Populate checklist
    private void PopulateAssignedData(ServiceRequestForm servicerequestform)
    {
        var allchecklist = db.checkLists;
        var srfchecklist = new HashSet<int>(servicerequestform.checkLists.Select(b => b.checkListId));
        var viewModel = new List<srfChecklistVM>();
        foreach (var check in allchecklist)
        {
            viewModel.Add(new srfChecklistVM
            {
                checkListId = check.checkListId,
                checkList1 = check.checkList1,
                Assigned = srfchecklist.Contains(check.checkListId)
            });
        }
        ViewBag.checkLists = viewModel;
    }

HTML

<tr>
@{
  <td colspan="2"><a href="#" class="toggler" data-prod-cat="1"><i class="fa fa-window-maximize" id="expand"></i><label>&nbsp; CheckList</label></a></td>
  <td colspan="2"><label>Remarks</label></td>
  int cnt = 0;

  List<SRF.Models.srfChecklistVM> checklists = ViewBag.checkLists;
  foreach (var check in checklists)
  {
    if (cnt++ % 1 == 0)
    {
     @:</tr><tr class="cat1" style="display:none">
    }
     @:
     <td colspan="2">
     <input type="checkbox" name="selectedChecklist" value="@check.checkListId" class="largerCheckbox"
      @(Html.Raw(check.Assigned ? "checked=\"checked\"" : "")) />
     @check.checkList1
     </td>
     @:
     //textbox for remarks was populated but does not save in database
     <td colspan="2"><input type="text" name="selectedChecklist" value="@check.remarks"></td>
     }
     @:</tr>
     }

First, i wanna know what will be my new model, second is how can i populate it and last is how to save it in database. Any help would be greatly appreciated!

identity01
  • 13
  • 3
  • You need to create a model containing a `bool` property for the checkbox and a `string` property for the remarks, and pass a collection of that model to a view, and generate your view correctly using `HtmlHelper` methods in a `for` loop or using an `EditorTemplate` –  Aug 30 '18 at 02:18
  • Refer [Pass List of Checkboxes into View and Pull out IEnumerable](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Aug 30 '18 at 02:20
  • on what model will i put my textboxes? – identity01 Aug 30 '18 at 02:26
  • Look ate the link I gave you - there is a view model named RoleVM` - in you case you would have a `string Remarks` property in that view model and use `@Html.TextAreaFor()` to generate the input –  Aug 30 '18 at 02:28
  • Use a viewmodel property which has type of `List`, iterate from that with `for` loop and bound `CheckBoxFor` and `TextAreaFor` from there, or use `EditorTemplate` containing both of them. – Tetsuya Yamamoto Aug 30 '18 at 02:28
  • i added public string remarks{get;set} on srfChecklistVM but this is just a viewmodel it is not mapped on my database – identity01 Aug 30 '18 at 02:50
  • textbox was already populated, but it is not saving in my database – identity01 Aug 30 '18 at 02:59

0 Answers0