Jaykumar Suthar

Jaykumar Suthar

  • NA
  • 207
  • 16.9k

how to set dropdown value in Edit mode of User Form in MVC.

Sep 10 2018 4:45 AM
Hello,
 
I m trying to set dropdown value in edit mode from model, but not able to do that,
Please help.
 
my code is as per below.
 
View
 
@model AdminPortal.Models.UserModel
@{
}
<script type="text/javascript">
$(function () {
valueChanged();
$('.datepicker').datepicker({
autoclose: true
})
debugger;
var ddlDesignation = $("#ddlDesignationId");
debugger;
$.ajax({
type: "POST",
url: "/User/PopulateDesignation",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
ddlDesignation.empty().append('<option selected="selected" value="0">--Select--</option>');
$.each(response, function () {
ddlDesignation.append($("<option></option>").val(this['Value']).html(this['Text']));
});
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
<label for="Designation">Designation</label>
@Html.DropDownListFor(Model => Model.DesignationId, new List<SelectListItem>
{
}, htmlAttributes: new { @class = "form-control", id = "ddlDesignationId" })
}
 
In model
 
public string DesignationId { get; set; }
In Controller
public ActionResult Create(string UserId)
{
//PopulateDesignation();
if (UserId == null)
return View();
else
{
UserDAL userDAL = new UserDAL();
return View(userDAL.GetUsers().Find(umodel => umodel.UserId == UserId));
}
}
public JsonResult PopulateDesignation()
{
List<SelectListItem> items = new List<SelectListItem>();
UserDAL userDAL = new UserDAL();
items = userDAL.PopulateDesignation();
return Json(items);
}

Answers (3)