Hi,
I'm having an issue with the Kendo Grid, using the MVC extensions.
I have a foreign key column defined which displays perfectly when the grid is shown. However, on edit, the dropdown (provided by a copy of the GridForeignKey.cshtml editor template) does not select the correct value for the record. This is the case whether we're using InCell, InLine or Popup editing.
To clarify, the dropdown shows all the correct available values, but the default is selected instead of the value in the record.
ViewModels:
Index View:
RegionForeignKey Partial (in Views/Branch/EditorTemplates/):
Note: this is what I'm using now, however if I revert to the GridForeignKey editor template the behaviour is the same.
Controller Actions:
I've scoured a number of other forum posts and haven't been able to find an answer. Any help you could give would be very welcome.
I'm having an issue with the Kendo Grid, using the MVC extensions.
I have a foreign key column defined which displays perfectly when the grid is shown. However, on edit, the dropdown (provided by a copy of the GridForeignKey.cshtml editor template) does not select the correct value for the record. This is the case whether we're using InCell, InLine or Popup editing.
To clarify, the dropdown shows all the correct available values, but the default is selected instead of the value in the record.
ViewModels:
public
class
BranchesViewModel
{
public
IQueryable<BranchViewModel> Branches {
get
;
set
; }
public
IEnumerable<RegionViewModel> Regions {
get
;
set
; }
}
public
class
BranchViewModel
{
[Editable(
false
, AllowInitialValue =
true
)]
[Required]
[Display(Name=
"Id"
)]
[HiddenInput(DisplayValue =
false
)]
public
int
BranchId {
get
;
set
; }
[Display(Name=
"GL Code"
, Order=2)]
[Required]
public
string
GLCode {
get
;
set
; }
[Display(Name=
"Region"
, Order=3)]
[Required]
[UIHint(
"RegionForeignKey"
)]
public
int
RegionId {
get
;
set
; }
[Display(Name=
"Branch Name"
, Order=1)]
[Required]
public
string
BranchName {
get
;
set
; }
}
public
class
RegionViewModel
{
public
int
RegionId {
get
;
set
; }
public
string
RegionName {
get
;
set
; }
}
Index View:
<
h2
>Branches</
h2
>
@(Html.Kendo().Grid<
BranchViewModel
>()
.Name("Branch")
.Columns(col =>
{
col.Bound(m => m.BranchName);
col.Bound(m => m.GLCode).Width(200);
col.ForeignKey(m => m.RegionId, Model.Regions, "RegionId", "RegionName").Width(180);
col.Command(cmd => { cmd.Edit(); cmd.Destroy(); }).Width(180);
})
.ToolBar(toolBar => toolBar.Create().Text("New Branch"))
.Editable(edit =>
{
edit.Mode(GridEditMode.InLine);
edit.DisplayDeleteConfirmation(true);
})
.Sortable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("errorHandler"))
.Model(model => model.Id(b => b.BranchId))
.Read(read => read.Action("Branch_Read", "Branch"))
.Create(create => create.Action("Branch_Create", "Branch"))
.Update(update => update.Action("Branch_Update", "Branch"))
.Destroy(destroy => destroy.Action("Branch_Delete", "Branch"))
)
)
RegionForeignKey Partial (in Views/Branch/EditorTemplates/):
Note: this is what I'm using now, however if I revert to the GridForeignKey editor template the behaviour is the same.
@model int
@(Html.Kendo().DropDownListFor(m => m)
.BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
)
Controller Actions:
public
ActionResult Index()
{
var model =
new
BranchesViewModel
{
Branches = _branchRepository.Get().Select(
b =>
new
BranchViewModel
{
BranchId = b.Id,
GLCode = b.GLCode,
BranchName = b.BranchName,
RegionId = b.RegionId
}
),
Regions = _regionRepository.Get().Select(
r =>
new
RegionViewModel
{
RegionId = r.Id,
RegionName = r.RegionName
}
).ToList()
};
return
View(model);
}
// Ajax for branches
public
ActionResult Branch_Read([DataSourceRequest] DataSourceRequest request)
{
var branches = _branchRepository.Get().Select(
b =>
new
BranchViewModel
{
BranchId = b.Id,
GLCode = b.GLCode,
BranchName = b.BranchName,
RegionId = b.RegionId
}
);
return
Json(branches.ToDataSourceResult(request));
}
I've scoured a number of other forum posts and haven't been able to find an answer. Any help you could give would be very welcome.