So, I've tried to get this working following many threads and articles and I can't get it to work. The value posts back to the controller fine, but I can't get it to set the default selected item on the DropDownList nor can I get it to display the text of the selected item when I select an option. If I use a client template to try and grab the text, it throws an error (Cannot read property 'Text' of null) and won't show any items in the grid. For this one, I'm not using just an IEnumerable list of ViewModels (like most of the examples show) to populate the DropDownList, I'm using a SelectList. I have it working fine in another project with an IEnumerable list of ViewModels, but I just can get it working with a SelectList and I don't want to create a ViewModel for these static options. Please help! Thanks!
Here's my grid:
@(Html.Kendo().Grid<Tracker.Areas.Admin.Models.AdHocLocationViewModel>()
.Name(
"gridAdHocLocations"
)
.Columns(columns =>
{
columns.Bound(c => c.LocationName).Title(
"Reviewer Location"
);
columns.Bound(c => c.Packet).Title(
"Added to"
);
columns.Bound(c => c.CreateDt).Title(
"Date Added"
).Width(120).Format(
"{0:MM/dd/yyyy}"
);
columns.Bound(c => c.Action).Title(
"Selection"
).ClientTemplate(
"#=Action.Text#"
).Width(180);
})
.Editable(e => e.Mode(GridEditMode.InCell))
.ToolBar(t => t.Save())
.Sortable()
.Pageable(pageable => pageable
.Refresh(
true
)
.PageSizes(
true
)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Batch(
true
)
.ServerOperation(
false
)
.Model(s =>
{
s.Id(model => model.LocationId);
s.Field(f => f.LocationName).Editable(
false
);
s.Field(f => f.Packet).Editable(
false
);
s.Field(f => f.CreateDt).Editable(
false
);
s.Field(p => p.Action).DefaultValue(
ViewData[
"defaultAction"
]
as
SelectListItem);
})
.Read(read => read.Action(
"LocationsAdHoc_Read"
,
"Location"
))
.Update(update => update.Action(
"LocationsAdHoc_Update"
,
"Location"
))
)
)
Here's my editor (I've also tried it strongly typed using DropDownListFor):
@(Html.Kendo().DropDownList()
.Name(
"Action"
)
.DataValueField(
"Value"
)
.DataTextField(
"Text"
)
.OptionLabel(
"Select Action"
)
.BindTo((SelectList)ViewData[
"actions"
])
)
Here's my model:
public
class
AdHocLocationViewModel
{
[ScaffoldColumn(
false
)]
public
int
LocationId {
get
;
set
; }
public
string
LocationName {
get
;
set
; }
public
string
Packet {
get
;
set
; }
public
int
ActionId {
get
;
set
; }
public
DateTime CreateDt {
get
;
set
; }
[UIHint(
"AdHocLocationEditor"
)]
public
SelectListItem Action {
get
;
set
; }
}