1
votes

I want to update dropdownlist value dynamically When Open is clicked.When user clicks 1st row then its respective data should display in dropdown and the same way for row 2. Actually, I want to use Json to update value inside dropdown in spite of loading the whole partial.

public PartialViewResult _ModalPopup( string Id)
    {
       EmpViewModel model=new EmpViewModel();
       Id=Id??"1";            
       var Listdata = context.Employees.Where(x => x.id == Id).Select(y => y.Category).Distinct().ToList();
       model.CategoriesList = new SelectList(Listdata);                    
        return PartialView("_MEmpModal", model);
    }

View

<table>
       <tr>
           <td>
              @Html.DisplayName("IT")
             </td>
             <td>
      <a class="LinkId" data-toggle="modal"  data-url="/Home/_ModalPopup?Id=1">Open</a>
        </td>
           </tr>
              <tr>
               <td>
                 @Html.DisplayName("Sales")
                   </td>
                    <td>
        <a class="LinkId" data-toggle="modal"  data-url="/Home/_ModalPopup?Id=2">Open</a>

                  </td>
                </tr>                            
        </table>
    @{Html.RenderAction("__MEmpModal"); }

Partial View

<div class="modal fade" id="DisplayModal" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    @Html.DropDownListFor(m => m.Category, Model.CategoriesList, new { @class = "form-control" })
                </div>               
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary pull-right">Save</button>
            </div>
        </div>
    </div>
</div>

Script

$(document).on("click", '.LinkId', function (e) {            
        $.ajax({
            url: $(this).data("url"),
            type: "GET",
        }).done(function (partialViewResult) {
            $("#DisplayModal").replacewith(partialViewResult);
            $('#DisplayModal').focus();
        });
    });
1

1 Answers

1
votes

Many thanks to @StephenMuecke, I followed his shown example about how to append data in the dropdownlist dynamically. This is the Script which solved my problem. I made small changes in Controller to return Json and then used this Script.

Controller

public JsonResult _ModalPopup( string Id)
    {
       EmpViewModel model=new EmpViewModel();
       Id=Id??"1";            
       var Listdata = context.Employees.Where(x => x.id == Id).Select(y => y.Category).Distinct().ToList();
       model.CategoriesList = new SelectList(Listdata);                    
        return Json(model.CategoriesList,JsonRequestBehavior.AllowGet);
    }

Script

 $(document).on("click", '.LinkId', function () {
            var url = $(this).data("url");
            var Category = $("#Category");
            $.getJSON(url, { id: $(this).val() }, function (response) {
                Category.empty().append($('<option></option>').val('').text('Please select'));
                $.each(response, function (index, item) {
                    Category.append($('<option></option>').val(item.Value).text(item.Text));
                });
                $('#DisplayModal').modal('show');
            });
        })