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">×</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();
});
});