hello
I'm new to kendo and was testing to see if buy the product while I'm making examples to see if I can use
I have a problem. I'm trying to do the ForeignKeyColumn example but canĀ“t get the combobox when running
it show me a textbox (where should be the combo for my foreingkey) and
I can edit it and change the id and grabs fine the data. but I need a combo and i know something is
missing but can figure what
can you help please?
My View
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.CiudadViewModel>()
.Name("Grid")
.Columns(columns => {
columns.Bound(p => p.id);
columns.ForeignKey(p => p.empresa, (System.Collections.IEnumerable)ViewData["employees"], "id", "descripcion");
columns.Bound(p => p.descripcion);
})
.ToolBar(toolBar => toolBar.Save())
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("errorHandler"))
.Model(model => {
model.Id(p => p.id);
model.Field(p => p.id).Editable(false);
})
.Read(read => read.Action("ForeignKeyColumn_Read", "Home"))
.Update(update => update.Action("ForeignKeyColumn_Update", "Home"))
)
)
MY VIEW MODEL
public class CiudadViewModel
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int id { get; set; }
public string descripcion { get; set; }
public int empresa { get; set; }
[UIHint("Empresas"), Required]
public EmpresaViewModel Empresa { get; set; }
}
MY CONTROLLER
public ActionResult GridForeingKey()
{
PopulateEmployees();
return View();
}
private void PopulateEmployees()
{
ViewData["employees"] = new EFDbContext().Empresas
.Select(e => new EmpresaViewModel()
{
id = e.id,
descripcion = e.descripcion
})
.OrderBy(e => e.descripcion);
}
public ActionResult ForeignKeyColumn_Read([DataSourceRequest] DataSourceRequest request)
{
return Json(SessionClientOrderRepository.All().ToDataSourceResult(request));
}
public class SessionClientOrderRepository
{
public static IEnumerable<CiudadViewModel> All()
{
IEnumerable<CiudadViewModel> result = HttpContext.Current.Session["ciudad"] as IEnumerable<CiudadViewModel>;
if (result == null)
{
HttpContext.Current.Session["ciudad"] = result = new EFDbContext().Ciudades
.Select(o => new CiudadViewModel
{
id = o.id,
descripcion = o.descripcion,
empresa = o.Empresa ,
Empresa = new EmpresaViewModel
{
descripcion = o.descripcion,
id = o.id
}
}).ToList();
}
return result;
}
}
I'm new to kendo and was testing to see if buy the product while I'm making examples to see if I can use
I have a problem. I'm trying to do the ForeignKeyColumn example but canĀ“t get the combobox when running
it show me a textbox (where should be the combo for my foreingkey) and
I can edit it and change the id and grabs fine the data. but I need a combo and i know something is
missing but can figure what
can you help please?
My View
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.CiudadViewModel>()
.Name("Grid")
.Columns(columns => {
columns.Bound(p => p.id);
columns.ForeignKey(p => p.empresa, (System.Collections.IEnumerable)ViewData["employees"], "id", "descripcion");
columns.Bound(p => p.descripcion);
})
.ToolBar(toolBar => toolBar.Save())
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("errorHandler"))
.Model(model => {
model.Id(p => p.id);
model.Field(p => p.id).Editable(false);
})
.Read(read => read.Action("ForeignKeyColumn_Read", "Home"))
.Update(update => update.Action("ForeignKeyColumn_Update", "Home"))
)
)
MY VIEW MODEL
public class CiudadViewModel
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int id { get; set; }
public string descripcion { get; set; }
public int empresa { get; set; }
[UIHint("Empresas"), Required]
public EmpresaViewModel Empresa { get; set; }
}
MY CONTROLLER
public ActionResult GridForeingKey()
{
PopulateEmployees();
return View();
}
private void PopulateEmployees()
{
ViewData["employees"] = new EFDbContext().Empresas
.Select(e => new EmpresaViewModel()
{
id = e.id,
descripcion = e.descripcion
})
.OrderBy(e => e.descripcion);
}
public ActionResult ForeignKeyColumn_Read([DataSourceRequest] DataSourceRequest request)
{
return Json(SessionClientOrderRepository.All().ToDataSourceResult(request));
}
public class SessionClientOrderRepository
{
public static IEnumerable<CiudadViewModel> All()
{
IEnumerable<CiudadViewModel> result = HttpContext.Current.Session["ciudad"] as IEnumerable<CiudadViewModel>;
if (result == null)
{
HttpContext.Current.Session["ciudad"] = result = new EFDbContext().Ciudades
.Select(o => new CiudadViewModel
{
id = o.id,
descripcion = o.descripcion,
empresa = o.Empresa ,
Empresa = new EmpresaViewModel
{
descripcion = o.descripcion,
id = o.id
}
}).ToList();
}
return result;
}
}