My foreign key columns are displaying the value from the datasource even there is null value in the datasource from the grid?
The index method of the controller gets the datasource for the dropdowns:
var djel = from d in db.AspNetUsers
where d.DjelatnikSuradnik == "D"
orderby d.Prezime, d.Ime
select new
{
d.Id,
PrezimeIme = d.Prezime + ", " + d.Ime
};
var sur = from d in db.AspNetUsers
where d.DjelatnikSuradnik == "S"
orderby d.Prezime, d.Ime
select new
{
d.Id,
PrezimeIme = d.Prezime + ", " + d.Ime
};
ViewBag.dsDjelatnik = djel.ToList();
ViewBag.dsSuradnik = sur.ToList();
And then the GetPlan methog is getting the data for the grid:
public JsonResult GetPlan (int Godina)
{
using (var db = new BilijonEntities())
{
var source = from pl in db.Plan
join k in db.OsigKuca on pl.OsigKucaID equals k.OsigKucaID
join p in db.Proizvod on pl.ProizvodID equals p.ProizvodID into p1
from p2 in p1.DefaultIfEmpty()
join g in db.ProizvodGrupa on pl.GrupaID equals g.GrupaID into g1
from g2 in g1.DefaultIfEmpty()
where pl.Godina == Godina
orderby k.Naziv, g2.Naziv, p2.Naziv
select new
{
pl.PlanId,
pl.Godina,
pl.OsigKucaID,
pl.ProizvodID,
pl.GrupaID,
pl.Iznos,
pl.UserId,
pl.UserIdPromjena,
pl.DjelatId,
pl.SuradId,
pl.DjelatnikVlasnik,
pl.AgencijaID,
pl.LokacijaId,
pl.BrojPolica
};
return Json(source.ToList(), JsonRequestBehavior.AllowGet);
}
}
And this is the grid:
@(Html.EJ().Grid<object>("gridPlan")
.Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dsPlan)
.UpdateURL("NormalUpdate")
.InsertURL("NormalInsert")
.RemoveURL("NormalDelete")
.Adaptor(AdaptorType.RemoteSaveAdaptor))
.AllowPaging()
.AllowSelection(true)
.AllowResizing()
.AllowSorting(true)
.AllowTextWrap(true)
.Locale("hr-HR")
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().ShowDeleteConfirmDialog().AllowEditing(); })
.AllowFiltering(true)
.FilterSettings(filter => { filter.FilterType(FilterType.Excel); })
.ToolbarSettings(toolbar =>
{
toolbar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Add);
items.AddTool(ToolBarItems.Edit);
items.AddTool(ToolBarItems.Delete);
items.AddTool(ToolBarItems.Update);
items.AddTool(ToolBarItems.Cancel);
items.AddTool(ToolBarItems.Search);
});
})
.Columns(col =>
{
col.Field("PlanId").HeaderText("PlanId").IsPrimaryKey(true).IsIdentity(true).Visible(false).TextAlign(TextAlign.Right).Width(75).Add();
col.Field("Godina").HeaderText("Godina").Visible(false).Width(55).Add();
col.Field("OsigKucaID").HeaderText("Osig. kuća").ValidationRules(v => v.AddRule("required", true)).ForeignKeyField("OsigKucaID").ForeignKeyValue("Naziv").EditType(EditingType.DropdownEdit).DataSource((IEnumerable<object>)ViewBag.dsKuce).Width(110).Add();
col.Field("AgencijaID").HeaderText("Agencija").ForeignKeyField("AgencijaID").ForeignKeyValue("Naziv").EditType(EditingType.DropdownEdit).DataSource((IEnumerable<object>)ViewBag.dsAgencija).Width(65).Add();
col.Field("LokacijaId").HeaderText("Lokacija").ForeignKeyField("LokacijaId").ForeignKeyValue("Naziv").EditType(EditingType.DropdownEdit).DataSource((IEnumerable<object>)ViewBag.dsLokacija).Width(80).Add();
col.Field("GrupaID").HeaderText("Proizvod grupa").ForeignKeyField("GrupaID").ForeignKeyValue("Naziv").EditType(EditingType.DropdownEdit).DataSource((IEnumerable<object>)ViewBag.dsGrupe).Width(60).Add();
col.Field("ProizvodID").HeaderText("Proizvod").ForeignKeyField("ProizvodID").ForeignKeyValue("Naziv").EditType(EditingType.DropdownEdit).DataSource((IEnumerable<object>)ViewBag.dsProizvodi).Width(80).Add();
col.Field("DjelatId").HeaderText("Djelatnik").ForeignKeyField("DjelatId").ForeignKeyValue("PrezimeIme").EditType(EditingType.DropdownEdit).DataSource((IEnumerable<object>)ViewBag.dsDjelatnik).Width(80).Add();
col.Field("DjelatnikVlasnik").HeaderText("Djelatnik vlasnik").HeaderTextAlign(TextAlign.Center).TextAlign(TextAlign.Center).DisplayAsCheckbox(true).DefaultValue(false).EditType(EditingType.BooleanEdit).Width("55px").Add();
col.Field("SuradId").HeaderText("Suradnik").ForeignKeyField("SuradId").ForeignKeyValue("PrezimeIme").EditType(EditingType.DropdownEdit).DataSource((IEnumerable<object>)ViewBag.dsSuradnik).Width(80).Add();
col.Field("Iznos").HeaderText("Iznos").TextAlign(TextAlign.Right).Width("60px").Format("{0:N2}").Add();
col.Field("BrojPolica").HeaderText("Broj").TextAlign(TextAlign.Right).Width("35px").Format("{0:N0}").Add();
col.Field("UserId").HeaderText("UserId").Visible(false).Width(55).Add();
col.Field("UserIdPromjena").HeaderText("UserIdPromjena").Visible(false).Width(55).Add();
}).ClientSideEvents(ev => ev.ActionBegin("gridBegin").ActionComplete("gridComplete"))
)
Thanks!
Bernard.