I've almost used up my trial time trying to figure out how to get a dropdownlist to show up like the example in:
http://demos.kendoui.com/web/grid/editing-custom.html
I see I'm not the only one struggling to get something that should be pretty simple to work. The example is missing an explanation of how the template #=Category.CategoryName# works and where to find the definition of that template. I'd like the see a step-by-step tutorial on how to wire up this control. I'm sure there are MANY others who would benefit from this as well.
In my demo application I have a database value I'm reading which is an integer which I want displayed as a text in a DropDownList within a grid. I think I'm close but I'm missing a few key points. How do I add my own editor template? The demo seems to suggest it is either in the view or controller for the grid. I think I found it in the "EditorTemplates" folder but have no idea how it gets connected to the view. Here are snippets of my project code:
My view:
@model IEnumerable<QT_Kendo2.Models.Isotope>
@{
ViewBag.Title = "Index";
}
<h2>Isotope Library</h2>
@(Html.Kendo().Grid(Model).Name("Isotopes").Columns(c =>
{
c.Bound(p => p.Id).Hidden();
c.Bound(p => p.Version).Hidden();
c.Bound(p => p.Name);
c.Bound(p => p.LLD).Format("{0:n1}");
c.Bound(p => p.ULD).Format("{0:n1}");
c.Bound(p => p.HalfLife);
c.Bound(p => p.HalfLife_Units).ClientTemplate("#=HLUnitList#"); // Convert the numeric index into text (hours, days, weeks, years)
c.Command(command => { command.Edit(); command.Destroy(); });
})
.ToolBar(toolbar =>
{
toolbar.Create();
// toolbar.Save(); // Used in batch mode
})
//.HtmlAttributes(new { style = "height:400px;" })
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Model(model =>
{
model.Id(p => p.Id);
model.Field(p => p.Id).Editable(false);
})
.Create(create => create.Action("EditingIsotope_Create", "Isotope")) // "Insert"
.Update(update => update.Action("EditingIsotope_Update", "Isotope"))
.Destroy(destroy => destroy.Action("EditingIsotope_Destroy", "Isotope"))
.Events(events => events.Error("error_handler"))
)
.Pageable()
.Sortable()
.Editable(editable => editable.Mode(GridEditMode.InLine))
)
Controller:
namespace QT_Kendo2.Controllers
{
public class IsotopeController : Controller
{
private QT_Db db = new QT_Db();
public ActionResult Index()
{
var hlUnits = new List<string>() {"hours", "days", "weeks", "years"};
ViewData["hlUnits"] = hlUnits;
return View(db.Isotopes.ToList());
}
public ActionResult EditingCustom_Read([DataSourceRequest] DataSourceRequest request)
{
var dsr = new DataSourceResult();
return Json(dsr);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingIsotope_Update([DataSourceRequest] DataSourceRequest request, Isotope isotope)
{
if (isotope != null && ModelState.IsValid)
{
db.Entry(isotope).State = EntityState.Modified;
db.SaveChanges();
}
return Json(ModelState.ToDataSourceResult());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingIsotope_Destroy([DataSourceRequest] DataSourceRequest request, Isotope isotope)
{
db.Isotopes.Remove(db.Isotopes.Find(isotope.Id));
db.SaveChanges();
return Json(ModelState.ToDataSourceResult());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingIsotope_Create([DataSourceRequest] DataSourceRequest request, Isotope isotope)
{
if (ModelState.IsValid)
{
db.Isotopes.Add(isotope);
db.SaveChanges();
}
return Json(ModelState.ToDataSourceResult());
}
EditorTemplates as seen in the demo. Not sure how to add these to my project. Just adding as a partial view doesn't work.
@model IEnumerable<QT_Kendo2.Models.Isotope>
@(Html.Kendo().DropDownListFor(m => m)
.Name("HLUnitList") // Name used by template? #= HLUnitList
.DataValueField("HalfLife_Units") // Name of the index from the model
.DataTextField("HLUnitList") // Name of the template again.
.BindTo((System.Collections.IEnumerable)ViewData["hlUnits"]) // Bind to the list index to the string list
)
http://demos.kendoui.com/web/grid/editing-custom.html
I see I'm not the only one struggling to get something that should be pretty simple to work. The example is missing an explanation of how the template #=Category.CategoryName# works and where to find the definition of that template. I'd like the see a step-by-step tutorial on how to wire up this control. I'm sure there are MANY others who would benefit from this as well.
In my demo application I have a database value I'm reading which is an integer which I want displayed as a text in a DropDownList within a grid. I think I'm close but I'm missing a few key points. How do I add my own editor template? The demo seems to suggest it is either in the view or controller for the grid. I think I found it in the "EditorTemplates" folder but have no idea how it gets connected to the view. Here are snippets of my project code:
My view:
@model IEnumerable<QT_Kendo2.Models.Isotope>
@{
ViewBag.Title = "Index";
}
<h2>Isotope Library</h2>
@(Html.Kendo().Grid(Model).Name("Isotopes").Columns(c =>
{
c.Bound(p => p.Id).Hidden();
c.Bound(p => p.Version).Hidden();
c.Bound(p => p.Name);
c.Bound(p => p.LLD).Format("{0:n1}");
c.Bound(p => p.ULD).Format("{0:n1}");
c.Bound(p => p.HalfLife);
c.Bound(p => p.HalfLife_Units).ClientTemplate("#=HLUnitList#"); // Convert the numeric index into text (hours, days, weeks, years)
c.Command(command => { command.Edit(); command.Destroy(); });
})
.ToolBar(toolbar =>
{
toolbar.Create();
// toolbar.Save(); // Used in batch mode
})
//.HtmlAttributes(new { style = "height:400px;" })
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Model(model =>
{
model.Id(p => p.Id);
model.Field(p => p.Id).Editable(false);
})
.Create(create => create.Action("EditingIsotope_Create", "Isotope")) // "Insert"
.Update(update => update.Action("EditingIsotope_Update", "Isotope"))
.Destroy(destroy => destroy.Action("EditingIsotope_Destroy", "Isotope"))
.Events(events => events.Error("error_handler"))
)
.Pageable()
.Sortable()
.Editable(editable => editable.Mode(GridEditMode.InLine))
)
Controller:
namespace QT_Kendo2.Controllers
{
public class IsotopeController : Controller
{
private QT_Db db = new QT_Db();
public ActionResult Index()
{
var hlUnits = new List<string>() {"hours", "days", "weeks", "years"};
ViewData["hlUnits"] = hlUnits;
return View(db.Isotopes.ToList());
}
public ActionResult EditingCustom_Read([DataSourceRequest] DataSourceRequest request)
{
var dsr = new DataSourceResult();
return Json(dsr);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingIsotope_Update([DataSourceRequest] DataSourceRequest request, Isotope isotope)
{
if (isotope != null && ModelState.IsValid)
{
db.Entry(isotope).State = EntityState.Modified;
db.SaveChanges();
}
return Json(ModelState.ToDataSourceResult());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingIsotope_Destroy([DataSourceRequest] DataSourceRequest request, Isotope isotope)
{
db.Isotopes.Remove(db.Isotopes.Find(isotope.Id));
db.SaveChanges();
return Json(ModelState.ToDataSourceResult());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingIsotope_Create([DataSourceRequest] DataSourceRequest request, Isotope isotope)
{
if (ModelState.IsValid)
{
db.Isotopes.Add(isotope);
db.SaveChanges();
}
return Json(ModelState.ToDataSourceResult());
}
EditorTemplates as seen in the demo. Not sure how to add these to my project. Just adding as a partial view doesn't work.
@model IEnumerable<QT_Kendo2.Models.Isotope>
@(Html.Kendo().DropDownListFor(m => m)
.Name("HLUnitList") // Name used by template? #= HLUnitList
.DataValueField("HalfLife_Units") // Name of the index from the model
.DataTextField("HLUnitList") // Name of the template again.
.BindTo((System.Collections.IEnumerable)ViewData["hlUnits"]) // Bind to the list index to the string list
)