mardi 25 juillet 2017

Fetching and Adding Checkbox dynamically on selection of Dropdown boxes

Form

I have a form as above. I am trying to work as when user selects Table Name & Permission, it goes back to server side, fetches the columns of the selected table & display all column names as check boxes. When use selects Save btn, HttpPost will execute and when user selects Cancel, to return back to home page.

I have created a ViewModel for this :

 // Actual EF Model
    public partial class TablePermission
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int PermissionLevelId { get; set; }
    public string TableName { get; set; }
    public string RestrictViewFields { get; set; }
    public string RestrictEditFields { get; set; }

    public virtual PermissionLevel PermissionLevel { get; set; }
    public virtual User User { get; set; }
}
// View Model for the View
public class TablePermissionsVM
{

    public TablePermissionsVM()
    {
        TablePermission = new TablePermission();
        RestrictViewFields = new List<FieldList>();

        // Created for trial to see Checkboxes 
        RestrictViewFields.Add(new FieldList() { FieldName = "UserId", Selected = false });
        RestrictViewFields.Add(new FieldList() { FieldName = "fName", Selected = false });
        RestrictViewFields.Add(new FieldList() { FieldName = "lName", Selected = false });

        RestrictEditFields = new List<FieldList>();
    }

    public TablePermission TablePermission { get; set; }

    public List<FieldList> RestrictViewFields { get; set; }

    public IEnumerable<FieldList> RestrictEditFields { get; set; }
}

// Model to save field names & it's selected status
public class FieldList
{
    public string FieldName { get; set;  }
    public bool Selected { get; set; }
}
}

Controller :

       [Authorize]
    [HttpGet]
    public ActionResult TablePermissions(TablePermissionsVM tablePermissionVm)
    {
        return View(tablePermissionVm);
    }

View - Just sharing the Checck box part :

        <div class="form-group">
            @Html.LabelFor(model => model.RestrictViewFields, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="editor-field">
                <ul>

                    @if (Model != null)
                    {
                        if (Model.RestrictViewFields != null)
                        {
                            foreach (var field in Model.RestrictViewFields)
                            {
                            <li>
                                <input id="field@(field.FieldName)"
                                       type="checkbox"
                                       name="SelectedViewFields"
                                       value="@field.FieldName"
                                       @(field.Selected ? "checked" : "") />
                                <label for="operator@(field.FieldName)">@field.FieldName</label>
                            </li>
                            }
                        @*@Html.CheckBoxFor()*@
                        }
                    }
                </ul>
            </div>
        </div>

Their are couple of things that I am not able to figure out & get confused with it. Mainly, on making sure that both the drop down boxes has value, I need to perform again a "Get" and fetch column names for the table selected & display the columns as check boxes. The way I have implemented Checkboxes, I will get proper selected Values in HttpPost, Right ! Are am I anywhere wrong ?

How to make the Get Request when both the drop down's are selected ??

Any help is highly appreciated. Thanks a lot in advance.




Aucun commentaire:

Enregistrer un commentaire