Hi
I am struggling to get a foreign key to display it's name in Kendo Grid. In my normal MVC views the name displays just fine.
I am using a code first approach in MVC with DBContext instead of using EDMX models.
Could someone please help me with this issue.
Help would be appreciated :)
Here is the Kendo Grid code for my view:
@(Html.Kendo().Grid<ZerothApp.Models.ProvinceViewModel>() .Name("grid") .Columns(columns => { columns.Bound(p => p.ProvinceName).Filterable(ftb => ftb.Multi(true).Search(true)); columns.Bound(p => p.Country).Filterable(ftb => ftb.Multi(true).Search(true)); columns.Bound(p => p.CreatedBy); columns.Bound(p => p.DateCreated); columns.Bound(p => p.LastEditedBy); columns.Bound(p => p.DateLastEdited); columns.Template(@<text></text>).ClientTemplate("<a class='editProvince' href='" + Url.Action("Edit", "Province") + "/#=ProvinceIdentifier#'>Edit</a> | <a class='detailsProvince' href='" + Url.Action("Details", "Province") + "/#=ProvinceIdentifier#'>Details</a> | <a class='deleteProvince' href='" + Url.Action("Delete", "Province") + "/#=ProvinceIdentifier#'>Delete</a>").Title("Actions"); }) .Pageable() .Sortable() .Groupable() .Scrollable() .Filterable() .HtmlAttributes(new { style = "height:550px;" }) .DataSource(dataSource => dataSource .Ajax() .Sort(sort => sort.Add("ProvinceName").Ascending()) .PageSize(20) .Model(model => { model.Id(p => p.ProvinceIdentifier); model.Field(p => p.ProvinceIdentifier).Editable(false); model.Field(p => p.CountryID).DefaultValue(1); }) .Read(read => read.Action("GetProvinces", "Province")) ) )
Here is the code for my ProvinceViewModel:
public class ProvinceViewModel { public System.Guid ProvinceIdentifier { get; set; } public string ProvinceName { get; set; } public System.Guid CountryID { get; set; } public string Country { get; set; } public string CreatedBy { get; set; } public Nullable<System.DateTime> DateCreated { get; set; } public string LastEditedBy { get; set; } public Nullable<System.DateTime> DateLastEdited { get; set; } public SelectList CountriesSelectList { get; set; } }
Here is the code for my Province class:
public class Province { [Key] public System.Guid ProvinceIdentifier { get; set; } public string ProvinceName { get; set; } public System.Guid CountryID { get; set; } public string CreatedBy { get; set; } public Nullable<System.DateTime> DateCreated { get; set; } public string LastEditedBy { get; set; } public Nullable<System.DateTime> DateLastEdited { get; set; } }
Here is the code for my ProvinceController:
using System;using ZerothApp.Models;using System.Linq;using System.Linq.Dynamic;using System.Web;using System.Web.Mvc;using Microsoft.AspNet.Identity.Owin;using DataTables.Mvc;using System.Collections.Generic;using System.Net;using System.Data.Entity;using System.Threading.Tasks;using Kendo.Mvc.Extensions;using Kendo.Mvc.UI;namespace ZerothApp.Controllers{ public class ProvinceController : Controller { private ApplicationDbContext _dbContext; public ApplicationDbContext DbContext { get { return _dbContext ?? HttpContext.GetOwinContext().Get<ApplicationDbContext>(); } private set { _dbContext = value; } } public ProvinceController() { } public ProvinceController(ApplicationDbContext dbContext) { _dbContext = dbContext; } // GET: Province public ActionResult Index() { return View(); } public ActionResult GetProvinces([DataSourceRequest] DataSourceRequest request) { return Json(GetProvinceViewModels().ToDataSourceResult(request)); } private IQueryable<ProvinceViewModel> GetProvinceViewModels() { return DbContext.Provinces .Select( c => new ProvinceViewModel { ProvinceIdentifier = c.ProvinceIdentifier, ProvinceName = c.ProvinceName, CountryID = c.CountryID, CreatedBy = c.CreatedBy, DateCreated = c.DateCreated, LastEditedBy = c.LastEditedBy, DateLastEdited = c.DateLastEdited }); } // GET: Province/Create public ActionResult Create() { var model = new ProvinceViewModel(); model.CountriesSelectList = GetCountriesSelectList(); return View("Add", model); } // POST: Province/Create [HttpPost] public async Task<ActionResult> Create(ProvinceViewModel provinceVM) { provinceVM.CountriesSelectList = GetCountriesSelectList(); if (!ModelState.IsValid) return View("Add", provinceVM); Province province = MaptoModel(provinceVM); DbContext.Provinces.Add(province); var task = DbContext.SaveChangesAsync(); await task; if (task.Exception != null) { ModelState.AddModelError("", "Unable to add the Asset"); return View("Add", provinceVM); } return RedirectToAction("Index"); } // GET: Province/Edit/5 public ActionResult Edit(Guid id) { var province = DbContext.Provinces.FirstOrDefault(x => x.ProvinceIdentifier == id); ProvinceViewModel provinceViewModel = MapToViewModel(province); if (Request.IsAjaxRequest()) return PartialView("Edit", provinceViewModel); return View(provinceViewModel); } // POST: Province/Edit/5 [HttpPost] public async Task<ActionResult> Edit(ProvinceViewModel provinceVM) { provinceVM.CountriesSelectList = GetCountriesSelectList(provinceVM.CountryID); if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return View(Request.IsAjaxRequest() ? "Edit" : "Edit", provinceVM); } Province province = MaptoModel(provinceVM); DbContext.Provinces.Attach(province); DbContext.Entry(province).State = EntityState.Modified; var task = DbContext.SaveChangesAsync(); await task; if (task.Exception != null) { ModelState.AddModelError("", "Unable to update the Asset"); Response.StatusCode = (int)HttpStatusCode.BadRequest; return View(Request.IsAjaxRequest() ? "Edit" : "Edit", provinceVM); } return RedirectToAction("Index"); } public async Task<ActionResult> Details(Guid id) { var province = await DbContext.Provinces.FirstOrDefaultAsync(x => x.ProvinceIdentifier == id); var provinceVM = MapToViewModel(province); if (Request.IsAjaxRequest()) return PartialView("Details", provinceVM); return View(provinceVM); } // GET: Province/Delete/5 public ActionResult Delete(Guid id) { var province = DbContext.Provinces.FirstOrDefault(x => x.ProvinceIdentifier == id); ProvinceViewModel provinceViewModel = MapToViewModel(province); if (Request.IsAjaxRequest()) return PartialView("Delete", provinceViewModel); return View(provinceViewModel); } // POST: Province/Delete/5 [HttpPost, ActionName("Delete")] public async Task<ActionResult> DeleteProvince(Guid ProvinceIdentifier) { var province = new Province { ProvinceIdentifier = ProvinceIdentifier }; DbContext.Provinces.Attach(province); DbContext.Provinces.Remove(province); var task = DbContext.SaveChangesAsync(); await task; if (task.Exception != null) { ModelState.AddModelError("", "Unable to Delete the Province"); Response.StatusCode = (int)HttpStatusCode.BadRequest; ProvinceViewModel provinceVM = MapToViewModel(province); return View(Request.IsAjaxRequest() ? "Delete" : "Delete", provinceVM); } return RedirectToAction("Index"); } private SelectList GetCountriesSelectList(object selectedValue = null) { return new SelectList(DbContext.Countries .Select(x => new { x.CountryIdentifier, x.CountryName }), "CountryIdentifier", "CountryName", selectedValue); } private ProvinceViewModel MapToViewModel(Province province) { var country = DbContext.Countries.Where(x => x.CountryIdentifier == province.CountryID).FirstOrDefault(); ProvinceViewModel provinceViewModel = new ProvinceViewModel() { ProvinceIdentifier = province.ProvinceIdentifier, ProvinceName = province.ProvinceName, CountryID = province.CountryID, CreatedBy = province.CreatedBy, DateCreated = province.DateCreated, LastEditedBy = province.LastEditedBy, DateLastEdited = province.DateLastEdited, Country = country != null ? country.CountryName : String.Empty, CountriesSelectList = new SelectList(DbContext.Countries .Select(x => new { x.CountryIdentifier, x.CountryName }), "CountryIdentifier", "CountryName", province.CountryID) }; return provinceViewModel; } private Province MaptoModel(ProvinceViewModel provinceVM) { Province province = new Province() { ProvinceIdentifier = provinceVM.ProvinceIdentifier, ProvinceName = provinceVM.ProvinceName, CountryID = provinceVM.CountryID, CreatedBy = provinceVM.CreatedBy, DateCreated = provinceVM.DateCreated, LastEditedBy = provinceVM.LastEditedBy, DateLastEdited = provinceVM.DateLastEdited }; return province; } }}
Here is the code for my CountryViewModel:
public class CountryViewModel { [Required] public System.Guid CountryIdentifier { get; set; } public string CountryName { get; set; } public string CreatedBy { get; set; } public Nullable<System.DateTime> DateCreated { get; set; } public string LastEditedBy { get; set; } public Nullable<System.DateTime> DateLastEdited { get; set; } }
Here is the code for my Country class:
public class Country { [Key] public System.Guid CountryIdentifier { get; set; } public string CountryName { get; set; } public string CreatedBy { get; set; } public Nullable<System.DateTime> DateCreated { get; set; } public string LastEditedBy { get; set; } public Nullable<System.DateTime> DateLastEdited { get; set; } }
And here is the code for my CountriesController:
using System;using ZerothApp.Models;using System.Linq;using System.Linq.Dynamic;using System.Web;using System.Web.Mvc;using Microsoft.AspNet.Identity.Owin;using DataTables.Mvc;using System.Collections.Generic;using System.Net;using System.Data.Entity;using System.Threading.Tasks;using Kendo.Mvc.Extensions;using Kendo.Mvc.UI;namespace ZerothApp.Controllers{ public class CountryController : Controller { private ApplicationDbContext _dbContext; public ApplicationDbContext DbContext { get { return _dbContext ?? HttpContext.GetOwinContext().Get<ApplicationDbContext>(); } private set { _dbContext = value; } } public CountryController() { } public CountryController(ApplicationDbContext dbContext) { _dbContext = dbContext; } // GET: Countries public ActionResult Index() { return View(); } public ActionResult GetCountries([DataSourceRequest] DataSourceRequest request) { return Json(GetCountryViewModels().ToDataSourceResult(request)); } private IQueryable<CountryViewModel> GetCountryViewModels() { return DbContext.Countries .Select( c => new CountryViewModel { CountryIdentifier = c.CountryIdentifier, CountryName = c.CountryName, CreatedBy = c.CreatedBy, DateCreated = c.DateCreated, LastEditedBy = c.LastEditedBy, DateLastEdited = c.DateLastEdited }); } // GET: Countries/Create public ActionResult Create() { var model = new CountryViewModel(); return View("Add", model); } // POST: Countries/Create [HttpPost] public async Task<ActionResult> Create(CountryViewModel countryVM) { if (!ModelState.IsValid) return View("Add", countryVM); Country country = MaptoModel(countryVM); DbContext.Countries.Add(country); var task = DbContext.SaveChangesAsync(); await task; if (task.Exception != null) { ModelState.AddModelError("", "Unable to add the country"); return View("Add", countryVM); } return RedirectToAction("Index"); } // GET: Countries/Edit/5 public ActionResult Edit(Guid id) { var country = DbContext.Countries.FirstOrDefault(x => x.CountryIdentifier == id); CountryViewModel countryViewModel = MapToViewModel(country); if (Request.IsAjaxRequest()) return PartialView("Edit", countryViewModel); return View(countryViewModel); } // POST: Countries/Edit/5 [HttpPost] public async Task<ActionResult> Edit(CountryViewModel countryVM) { if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return View(Request.IsAjaxRequest() ? "Edit" : "Edit", countryVM); } Country country = MaptoModel(countryVM); DbContext.Countries.Attach(country); DbContext.Entry(country).State = EntityState.Modified; var task = DbContext.SaveChangesAsync(); await task; if (task.Exception != null) { ModelState.AddModelError("", "Unable to update the Asset"); Response.StatusCode = (int)HttpStatusCode.BadRequest; return View(Request.IsAjaxRequest() ? "Edit" : "Edit", countryVM); } return RedirectToAction("Index"); } public async Task<ActionResult> Details(Guid id) { var country = await DbContext.Countries.FirstOrDefaultAsync(x => x.CountryIdentifier == id); var countryVM = MapToViewModel(country); if (Request.IsAjaxRequest()) return PartialView("Details", countryVM); return View(countryVM); } // GET: Countries/Delete/5 public ActionResult Delete(Guid id) { var country = DbContext.Countries.FirstOrDefault(x => x.CountryIdentifier == id); CountryViewModel countryViewModel = MapToViewModel(country); if (Request.IsAjaxRequest()) return PartialView("Delete", countryViewModel); return View(countryViewModel); } // POST: Countries/Delete/5 [HttpPost, ActionName("Delete")] public async Task<ActionResult> DeleteCountry(Guid CountryIdentifier) { var country = new Country { CountryIdentifier = CountryIdentifier }; DbContext.Countries.Attach(country); DbContext.Countries.Remove(country); var task = DbContext.SaveChangesAsync(); await task; if (task.Exception != null) { ModelState.AddModelError("", "Unable to Delete the country"); Response.StatusCode = (int)HttpStatusCode.BadRequest; CountryViewModel countryVM = MapToViewModel(country); return View(Request.IsAjaxRequest() ? "Delete" : "Delete", countryVM); } return RedirectToAction("Index"); } private CountryViewModel MapToViewModel(Country country) { CountryViewModel countryViewModel = new CountryViewModel() { CountryIdentifier = country.CountryIdentifier, CountryName = country.CountryName, CreatedBy = country.CreatedBy, DateCreated = country.DateCreated, LastEditedBy = country.LastEditedBy, DateLastEdited = country.DateLastEdited, }; return countryViewModel; } private Country MaptoModel(CountryViewModel countryVM) { Country country = new Country() { CountryIdentifier = countryVM.CountryIdentifier, CountryName = countryVM.CountryName, CreatedBy = countryVM.CreatedBy, DateCreated = countryVM.DateCreated, LastEditedBy = countryVM.LastEditedBy, DateLastEdited = countryVM.DateLastEdited }; return country; } }}