I can't seem to get a drop down Editor Template working on my grid's popup editor. I am using a ViewModel rather than using my Entity Model directly, maybe that is part of the problem? I cannot use the Model directly as the database schema does not match the expected pattern, and one is supposed to always use a ViewModel I am told.
Here is what I've got:
View Models
The UIHint is added as required and I have the quarter property linked to the Quarter object per the pattern.
public
class
AquisitionNotesViewModel
{
public
int
noteid {
get
;
set
; }
public
int
sourceid {
get
;
set
; }
public
int
? cyear {
get
;
set
; }
[UIHint(
"QuarterDropDown"
)]
public
Quarter quarter {
get
;
set
; }
[Column(TypeName =
"text"
)]
public
string
note {
get
;
set
; }
[Column(TypeName =
"date"
)]
public
DateTime? datetime {
get
;
set
; }
}
public
class
Quarter
{
public
int
? quarter {
get
;
set
; }
}
Controller
The controller is returning a List of Quarter objects with valid values as well as the ViewModel to populate the grid.
public
ActionResult AquisitionNotes_Read([DataSourceRequest] DataSourceRequest request,
int
sourceid)
{
IList<Quarter> quartersList =
new
List<Quarter>();
quartersList.Add(
new
Quarter { quarter = 1 });
quartersList.Add(
new
Quarter { quarter = 2 });
quartersList.Add(
new
Quarter { quarter = 3 });
quartersList.Add(
new
Quarter { quarter = 4 });
ViewData[
"quarters"
] = quartersList;
IList<AquisitionNotesViewModel> notesVM =
new
List<AquisitionNotesViewModel>();
var Query = from notes
in
db.acquisitionnotes
where notes.sourceid == sourceid
select
new
{
noteid = notes.noteid,
sourceid = notes.sourceid,
quarter = notes.quarter,
cyear = notes.cyear,
note = notes.note,
datetime = notes.datetime
};
foreach
( var note
in
Query) {
notesVM.Add(
new
AquisitionNotesViewModel
{
noteid = note.noteid,
sourceid = note.sourceid,
quarter =
new
Quarter { quarter = note.quarter },
cyear = note.cyear,
note = note.note,
datetime = note.datetime,
});
}
return
Json(notesVM.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
Editor Template (named QuarterDropDown)
Since I do not have separate values and IDs I set DataValueField and DataTextField to my field (property) name. And I bind to the List of quarters.
@(Html.Kendo().DropDownList()
.Name(
"quarter"
)
// Name of the widget should be the same as the name of the property
.DataValueField(
"quarter"
)
// The value of the dropdown is taken from the EmployeeID property
.DataTextField(
"quarter"
)
// The text of the items is taken from the EmployeeName property
.BindTo((System.Collections.IEnumerable)ViewData[
"quarters"
])
// A list of all employees which is populated in the controller
)
And finally my Grid
@(Html.Kendo().Grid<AquisitionNotesViewModel>()
.Name(GridID)
.Columns(columns =>
{
columns.Bound(p => p.datetime ).Format(
"{0:dd/MM/yyyy}"
);
columns.Bound(p => p.quarter.quarter ).Width(120);
columns.Bound(p => p.cyear).Width(50);
columns.Bound(p => p.note).Width(120);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(
new
{ style =
"height:350px;"
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Sort(sort => sort.Add(
"datetime"
).Ascending())
//.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.noteid ))
.Create(update => update.Action(
"AquisitionNotes_Create"
,
"AquisitionNotes"
))
.Read(read => read.Action(
"AquisitionNotes_Read"
,
"AquisitionNotes"
).Data(
"GetCurrentSourceID"
))
//.Read(read => read.Action("AquisitionNotes_Read", "AquisitionNotes", new { sourceid = 383}))
.Update(update => update.Action(
"AquisitionNotes_Upodate"
,
"AquisitionNotes"
))
.Destroy(update => update.Action(
"AquisitionNotes_Destroy"
,
"AquisitionNotes"
))
)
)
What am I missing or doing wrong?
Thanks, Brad