Terry

Terry

  • NA
  • 148
  • 0

MVC : Binding dynamically added Checkboxes to the Model ???

Jul 30 2017 6:13 PM
Hello,
 
    In my page, I have a form that is bound to a Model. Their is a DropDownFor for a property named "TableName". Onchange event of this dropdown, I call a javascript method which calls one of the Action from the Controller and adds checkboxes for a property "RestrictEditFields" of type List<FieldList>. Here's the View Code :
  1. using (Html.BeginForm("TablePermissions""Admin", FormMethod.Post, new { id = "tblPermForm" }))  
  2. {  
  3.     @Html.AntiForgeryToken()  
  4.   
  5.     <div class="form-horizontal">  
  6.   
  7.         <hr />  
  8.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  9.   
  10.         <div class="form-group">  
  11.             @Html.LabelFor(model => model.TablePermission.TableName, htmlAttributes: new { @class = "control-label col-md-2" })  
  12.             <div class="editor-field">  
  13.                 @Html.DropDownListFor(model => model.TablePermission.TableName,   
  14.                     DbUtilities.GetTableNames(), "Select Table",  
  15.                     new { @class = "form-control", @onchange="FillFields()" })  
  16.                 @Html.ValidationMessageFor(model => model.TablePermission.TableName, ""new { @class = "text-danger" })  
  17.             </div>  
  18.         </div>  
  19.   
  20.         <div class="form-group">  
  21.             @Html.LabelFor(model => model.RestrictEditFields, htmlAttributes: new { @class = "control-label col-md-2" })  
  22.             <div class="editor-field">  
  23.                 <table id="editTable">  
  24.   
  25.                 </table>  
  26.             </div>  
  27.         </div>  
  28.   
  29. }  
  30.   
  31.   
  32.   
  33. <script>  
  34.     function FillFields() {  
  35.         var tblName = $('#TablePermission_TableName').val();  
  36.         var tblPermLevel = $('#TablePermission_PermissionLevelId').val();  
  37.   
  38.         //($('tblPermForm').valid()) { ERROR - OBJECT DOESN'T HAVE valid()'  
  39.         if (tblName != null && tblPermLevel != null) {  
  40.             alert("Tbl Name = " + tblName + " Perm = " + tblPermLevel);  
  41.   
  42.             $.ajax({  
  43.                 url: '/Admin/FillFields',  
  44.                 type: 'GET',  
  45.                 dataType: "JSON",  
  46.                 data: { TableName: tblName, TablePermLevel: tblPermLevel },  
  47.                 success: function (jsonData) {  
  48.                     alert(jsonData);  
  49.                     var restrictViewList = jsonData.RestrictViewList;  
  50.                     var restrictEditList = jsonData.RestrictEditList;  
  51.                     // Displays proper count of each List<FieldList>  
  52.                     alert("Count of View = " + $(jsonData.RestrictViewList).length + " restrictEditList = " + $(jsonData.RestrictEditList).length);  
  53.   
  54.                     $("#editTable").html("");  // Clear before appending new ones  
  55.                     $.each(jsonData.RestrictEditList, function (i, field) {  
  56.                         $("#editTable").append('<input type="checkbox" name="edit-group" value="' + field.FieldName + '" /> ' + field.FieldName );  
  57.                           
  58.   
  59.                 },   
  60.                 error: function (xhr) {  
  61.                     // debugger  
  62.                     console.log(xhr.responseText);  
  63.                     alert("Error has occured");  
  64.                 }  
  65.             });  
  66.         }  
  67.     }  
  68. </script>  
FieldList class :
  1. public class FieldList  
  2. {  
  3.     public string FieldName { getset;  }  
  4.     public bool Selected { getset; }  
  5. }  
 
 The data received thru json, has a list of fields and I display all those fields as check box. What I want to achieve is - basically, all selected Fields of checkboxes should be saved in Model.RestrictEditFields. And when the user selects that particular table again, all the fields should be displayed and if the value of Selected is true of a FieldList, then that check box should be marked selected.
 
  I am a newbie in MVC and web, I don not get how do I implement this part. What would be the best approach for this ? After researching alot, I could finally add checkboxes dynamically. I am not able to figure out, how and where to bind it with the save & save to the model on clicking Save button. If anyone would help in providing guideline on how to proceed to achieve the goal, would be great.
 
Any help is highly appreciated.
 
Thanks a lot,
 

Answers (2)