OK, for my MVC Kendo grid I used a Poup Editor template, it was formatted kind of like this:
<div class="editor-field">
@Html.EditorFor(model => model.drugID)
@Html.ValidationMessageFor(model => model.drugID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.concentration)
@Html.ValidationMessageFor(model => model.concentration)
</div>
<script>
$("#drugID").change(function (e) {
var val = $("#drugID").val();
var obj = $("#concentration");
switch (val) {
case '8':
obj.val('1000');
break;
case '9':
obj.val('2500');
break;
case '10':
obj.val('5000');
break;
case '11':
obj.val('10000');
break;
default:
break;
}
});
</script>
The problem was, that when saving a new record, 0 resulted for the concentration field, even though the above javascript was setting it when an item was selected in the drug drop down.
The solution was to place the following code in the Save event of the grid:
function SQSaving(e) {
var val = $("#drugID").val();
var uid = $(".k-edit-form-container").closest("[data-role=window]").data("uid"),
model = $("#sqdosegrid").data("kendoGrid").dataSource.getByUid(uid);
switch (val) {
case '8':
model.set("concentration", 1000);
break;
case '9':
model.set("concentration", 2500);
break;
case '10':
model.set("concentration", 5000);
break;
case '11':
model.set("concentration", 10000);
break;
default:
break;
}
Snippet for the grid declaration:
@(Html.Kendo().Grid<Model.Models.Dose>()
.Name("sqdosegrid")
.Events(e =>
{
e.Edit("SQeditmode");
e.Save("SQSaving");//Handles validation cause Entity Required Attribute sucks
e.DataBound("dataBound");
.Editable(e =>
{
e.Mode(GridEditMode.PopUp).TemplateName("Popup");
e.Enabled(canEdit);
e.Window(w => w.Title("Subcutaneous Injection"));
})
})
No comments:
Post a Comment