I have popup form that called dropdown from controller that contain Json data, but i always get empty value for text.
The goal is post the id to context
I have someone can help for this one
public class DataController : Controller
{
private readonly ApplicationDbContext _context;
public DataController(ApplicationDbContext context)
{
_context = context;
}
public JsonResult GetDepartement()
{
List<IdTextPair> result = (
from m in _context.Departements
orderby m.DepertementId
select new IdTextPair()
{
Id = m.DepertementId,
Text = m.DepartementName
}).ToList();
return Json(new { data = result });
}
}
@Html.DropDownList("DepartementName", new SelectList(string.Empty, "Id", "Text"), "Please select..", new { @class = "form-control",style = "width:350px;"})
@Html.ValidationMessageFor(a=>a.DepartmentId)
$.getJSON('/Data/GetDepartement', null, function (data) {
$.each(data, function (index, obj) {
$('#DepartementName').append('<option value=' +
obj.id + '>' + obj.text + '</option>');
});
});