I've spent hours on this and I'm really stumped. I have a grid that should display dropdowns for two columns: OwnerID and State. The columns shows in the grid but not in Edit or New.
@(Html.Kendo().Grid<ClientViewModel>()
.Name("ClientGrid")
.Pageable()
.Columns(columns =>
{
columns.Bound(c => c.ClientID);
columns.Bound(c => c.ClientName);
columns.Bound(c => c.OwnerID).ClientTemplate("#:OwnerID.UserName #").Title("Owner").Width(155);
columns.Bound(c => c.Addr1).Visible(true);
columns.Bound(c => c.Addr2).Visible(true);
columns.Bound(c => c.City).Visible(true);
columns.Bound(c => c.State).ClientTemplate("#:State.StateName #").Title("State");
columns.Bound(c => c.Zip).Visible(true);
columns.Bound(c => c.SoldBy);
columns.Bound(c => c.Status);
columns.Bound(c => c.Phone);
columns.Bound(c => c.VolumnDiscountPct).Title("Vol. DC %").Visible(true);
columns.Bound(c => c.VolumnDiscountRange).Title("Vol. DC Range").Visible(true);
columns.Bound(c => c.VolumnDiscountRangeType).Title("Vol. DC Type").Visible(true);
columns.Bound(c => c.VolumnDiscountStartDate).Title("Vol. DC StartDate").Visible(true);
columns.Command(command => {command.Edit(); });
})
.DataSource(ds => ds
.Ajax()
.ServerOperation(false)
.Read(read => read.Action("GetClients", "Client"))
.Update(udt => udt.Action("ClientUpdate", "Client"))
.Create(c => c.Action("ClientCreate", "Client"))
//.Destroy(d => d.Action("ClientDelete", "Client"))
.Model(m =>
{
m.Id(ur => ur.ClientID);
m.Field(ur => ur.OwnerID).DefaultValue(
ViewData["defautOwner"] as CCProMVC.Models.CCProUser);
m.Field(ur => ur.State).DefaultValue(
ViewData["defaultState"] as CCProMVC.Models.StateModel);
}))
.Editable(e => e.Mode(GridEditMode.PopUp))
.ToolBar(t => t.Create())
)
Editors and their Controllers:
@model CCProMVC.Models.CCProUser
@(Html.Kendo().DropDownList()
.Name("OwnerID")
.DataTextField("UserName")
.DataValueField("Id")
.DataSource(d => d
.Read(r => r.Action("Index", "OwnerIDEditor"))
)
)
@model CCProMVC.Models.StateModel
@(Html.Kendo().DropDownList()
.Name("State")
.DataTextField("StateName")
.DataValueField("StateAbbrevation")
.DataSource(d => d
.Read(r => r.Action("Index", "StateEditor"))
)
)
public class OwnerIDEditorController : Controller
{
// GET: OwnerIDEditor
public JsonResult Index()
{
CCProEntities context = new CCProEntities();
var users = context.AspNetUsers.Select(u => new CCProMVC.Models.CCProUser {Id = u.Id, UserName = u.UserName});
return this.Json(users, JsonRequestBehavior.AllowGet);
}
}
public class StateEditorController : Controller
{
// GET: StateEditor
public JsonResult Index()
{
CCProEntities context = new CCProEntities();
var states = context.States.Select(s => new StateModel { StateAbbrevation = s.StateAbbrevation, StateName = s.StateName}).OrderBy(s => s.StateName);
return this.Json(states, JsonRequestBehavior.AllowGet);
}
}
Finally the main Controller:
public class ClientController : Controller
{
// GET: Client
public ActionResult Index()
{
CCProEntities dbContext = new CCProEntities();
ViewData["defaultState"] = dbContext.States.AsEnumerable().Select(s => new StateModel { StateAbbrevation = s.StateAbbrevation, StateName = s.StateName }).First();
ViewData["defautOwner"] = dbContext.AspNetUsers.AsEnumerable().Select(u => new CCProUser { Id = u.Id, UserName = u.UserName }).First();
return View("Client");
}
public JsonResult GetClients([DataSourceRequest]DataSourceRequest request)
{
CCProEntities dbContext = new CCProEntities();
var clientsQuery = dbContext.Clients.Select(c => new ClientViewModel
{
ClientID = c.ClientID,
ClientName = c.ClientName,
OwnerID = new CCProUser { Id = c.AspNetUser.Id, UserName = c.AspNetUser.UserName},
OwnershipDate = c.OwnershipDate,
Addr1 = c.Addr1,
Addr2 = c.Addr2,
City = c.City,
State = new StateModel { StateAbbrevation = c.State1.StateAbbrevation, StateName = c.State1.StateName },
Zip = c.Zip,
SoldBy = c.SoldBy,
Status = c.Status,
Phone = c.Phone,
VolumnDiscountPct = c.VolumnDiscountPct,
VolumnDiscountRange = c.VolumnDiscountRange,
VolumnDiscountRangeType = c.VolumnDiscountRangeType,
VolumnDiscountStartDate = c.VolumnDiscountStartDate
}).OrderBy(o => o.ClientName);
return Json(clientsQuery.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult ClientCreate([DataSourceRequest] DataSourceRequest request, Models.ClientViewModel client)
{
CCProMVC.Client eFClient = new Client();
var context = new CCProEntities();
if (client != null && ModelState.IsValid)
{
eFClient.ClientName = client.ClientName;
eFClient.Addr1 = client.Addr1;
eFClient.Addr2 = client.Addr2;
eFClient.City = client.City;
eFClient.State = client.State.StateAbbrevation;
eFClient.Zip = client.Zip;
eFClient.OwnerID = client.OwnerID.Id;
eFClient.Phone = client.Phone;
eFClient.SoldBy = client.SoldBy;
eFClient.VolumnDiscountPct = client.VolumnDiscountPct;
eFClient.VolumnDiscountRange = client.VolumnDiscountRange;
eFClient.VolumnDiscountRangeType = client.VolumnDiscountRangeType;
eFClient.VolumnDiscountStartDate = client.VolumnDiscountStartDate;
eFClient.OwnershipDate = client.OwnershipDate;
context.Clients.Add(eFClient);
context.SaveChanges();
}
return Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult());
}
}
I'm using Editor Templates and the Controllers for the editors when I set a breakpoint are not getting hit.
Any thoughts would be appreciated.
Greg
@(Html.Kendo().Grid<ClientViewModel>()
.Name("ClientGrid")
.Pageable()
.Columns(columns =>
{
columns.Bound(c => c.ClientID);
columns.Bound(c => c.ClientName);
columns.Bound(c => c.OwnerID).ClientTemplate("#:OwnerID.UserName #").Title("Owner").Width(155);
columns.Bound(c => c.Addr1).Visible(true);
columns.Bound(c => c.Addr2).Visible(true);
columns.Bound(c => c.City).Visible(true);
columns.Bound(c => c.State).ClientTemplate("#:State.StateName #").Title("State");
columns.Bound(c => c.Zip).Visible(true);
columns.Bound(c => c.SoldBy);
columns.Bound(c => c.Status);
columns.Bound(c => c.Phone);
columns.Bound(c => c.VolumnDiscountPct).Title("Vol. DC %").Visible(true);
columns.Bound(c => c.VolumnDiscountRange).Title("Vol. DC Range").Visible(true);
columns.Bound(c => c.VolumnDiscountRangeType).Title("Vol. DC Type").Visible(true);
columns.Bound(c => c.VolumnDiscountStartDate).Title("Vol. DC StartDate").Visible(true);
columns.Command(command => {command.Edit(); });
})
.DataSource(ds => ds
.Ajax()
.ServerOperation(false)
.Read(read => read.Action("GetClients", "Client"))
.Update(udt => udt.Action("ClientUpdate", "Client"))
.Create(c => c.Action("ClientCreate", "Client"))
//.Destroy(d => d.Action("ClientDelete", "Client"))
.Model(m =>
{
m.Id(ur => ur.ClientID);
m.Field(ur => ur.OwnerID).DefaultValue(
ViewData["defautOwner"] as CCProMVC.Models.CCProUser);
m.Field(ur => ur.State).DefaultValue(
ViewData["defaultState"] as CCProMVC.Models.StateModel);
}))
.Editable(e => e.Mode(GridEditMode.PopUp))
.ToolBar(t => t.Create())
)
Editors and their Controllers:
@model CCProMVC.Models.CCProUser
@(Html.Kendo().DropDownList()
.Name("OwnerID")
.DataTextField("UserName")
.DataValueField("Id")
.DataSource(d => d
.Read(r => r.Action("Index", "OwnerIDEditor"))
)
)
@model CCProMVC.Models.StateModel
@(Html.Kendo().DropDownList()
.Name("State")
.DataTextField("StateName")
.DataValueField("StateAbbrevation")
.DataSource(d => d
.Read(r => r.Action("Index", "StateEditor"))
)
)
public class OwnerIDEditorController : Controller
{
// GET: OwnerIDEditor
public JsonResult Index()
{
CCProEntities context = new CCProEntities();
var users = context.AspNetUsers.Select(u => new CCProMVC.Models.CCProUser {Id = u.Id, UserName = u.UserName});
return this.Json(users, JsonRequestBehavior.AllowGet);
}
}
public class StateEditorController : Controller
{
// GET: StateEditor
public JsonResult Index()
{
CCProEntities context = new CCProEntities();
var states = context.States.Select(s => new StateModel { StateAbbrevation = s.StateAbbrevation, StateName = s.StateName}).OrderBy(s => s.StateName);
return this.Json(states, JsonRequestBehavior.AllowGet);
}
}
Finally the main Controller:
public class ClientController : Controller
{
// GET: Client
public ActionResult Index()
{
CCProEntities dbContext = new CCProEntities();
ViewData["defaultState"] = dbContext.States.AsEnumerable().Select(s => new StateModel { StateAbbrevation = s.StateAbbrevation, StateName = s.StateName }).First();
ViewData["defautOwner"] = dbContext.AspNetUsers.AsEnumerable().Select(u => new CCProUser { Id = u.Id, UserName = u.UserName }).First();
return View("Client");
}
public JsonResult GetClients([DataSourceRequest]DataSourceRequest request)
{
CCProEntities dbContext = new CCProEntities();
var clientsQuery = dbContext.Clients.Select(c => new ClientViewModel
{
ClientID = c.ClientID,
ClientName = c.ClientName,
OwnerID = new CCProUser { Id = c.AspNetUser.Id, UserName = c.AspNetUser.UserName},
OwnershipDate = c.OwnershipDate,
Addr1 = c.Addr1,
Addr2 = c.Addr2,
City = c.City,
State = new StateModel { StateAbbrevation = c.State1.StateAbbrevation, StateName = c.State1.StateName },
Zip = c.Zip,
SoldBy = c.SoldBy,
Status = c.Status,
Phone = c.Phone,
VolumnDiscountPct = c.VolumnDiscountPct,
VolumnDiscountRange = c.VolumnDiscountRange,
VolumnDiscountRangeType = c.VolumnDiscountRangeType,
VolumnDiscountStartDate = c.VolumnDiscountStartDate
}).OrderBy(o => o.ClientName);
return Json(clientsQuery.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult ClientCreate([DataSourceRequest] DataSourceRequest request, Models.ClientViewModel client)
{
CCProMVC.Client eFClient = new Client();
var context = new CCProEntities();
if (client != null && ModelState.IsValid)
{
eFClient.ClientName = client.ClientName;
eFClient.Addr1 = client.Addr1;
eFClient.Addr2 = client.Addr2;
eFClient.City = client.City;
eFClient.State = client.State.StateAbbrevation;
eFClient.Zip = client.Zip;
eFClient.OwnerID = client.OwnerID.Id;
eFClient.Phone = client.Phone;
eFClient.SoldBy = client.SoldBy;
eFClient.VolumnDiscountPct = client.VolumnDiscountPct;
eFClient.VolumnDiscountRange = client.VolumnDiscountRange;
eFClient.VolumnDiscountRangeType = client.VolumnDiscountRangeType;
eFClient.VolumnDiscountStartDate = client.VolumnDiscountStartDate;
eFClient.OwnershipDate = client.OwnershipDate;
context.Clients.Add(eFClient);
context.SaveChanges();
}
return Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult());
}
}
I'm using Editor Templates and the Controllers for the editors when I set a breakpoint are not getting hit.
Any thoughts would be appreciated.
Greg