Dropdown Method
public ActionResult Index()
{
List<TblCountry> countries = _db.TblCountries.ToList();
List<TblDepartment> departments = _db.TblDepartments.ToList();
ViewBag.ConList = new SelectList(countries, "Country_Id", "Country_Name");
ViewBag.DepList = new SelectList(departments, "D_Id", "D_Name");
return View();
}
{
List<TblCountry> countries = _db.TblCountries.ToList();
List<TblDepartment> departments = _db.TblDepartments.ToList();
ViewBag.ConList = new SelectList(countries, "Country_Id", "Country_Name");
ViewBag.DepList = new SelectList(departments, "D_Id", "D_Name");
return View();
}
Multiple table show data list with viewmodel
public ActionResult ViewAll()
{
_db.Configuration.ProxyCreationEnabled = false;
List<VmClass> vm = _db.TblUsers.Select(x => new VmClass
{
User_Id = x.User_Id,
User_Name = x.User_Name,
User_Gender = x.User_Gender,
Country_Name = x.TblCountry.Country_Name,
D_Name = x.TblDepartment.D_Name,
}).ToList();
return Json(new { data = vm, status = "success" }, JsonRequestBehavior.AllowGet);
}
Dropdown On View
@Html.DropDownListFor(model => model.D_Id, ViewBag.DepList as SelectList, "Select Deppatment", new { @class = "form-control" })
Append data with Ajax and show List
function LoadData() {
$.ajax({
url: "/Home/ViewAll",
type: "Get",
dataType: "json",
success: function (result) {
$("#List").empty();
if ($("#List").empty()) {
$.each(result.data, function (key, value) {
var html =
"<tr>" +
"<td>" + value.User_Name + "</td>" +
"<td>" + value.User_Gender + "</td>" +
"<td>" + value.Country_Name + "</td>" +
"<td>" + value.D_Name + "</td>" +
"<td>" + ' <a href="#" class="btn btn-success" onclick="Edit(' + value.User_Id + ')">Edit</a>' + ' <a href="#" class="btn btn-danger" onclick="Delete(' + value.User_Id + ')">Delete</a>' + "</td>" +
"</tr>"
$("#List").append(html)
})
}
},
error: function () {
alert("error");
}
});
}
Post a Comment
If you have any doubt please let me know