简体   繁体   中英

Kendo MVC NumericTextBoxFor default value not binding to model

JobPlanViewModel

public class JobPlanViewModel : IMapFrom<JobPlan>, IJobPlan
{
  ...
  [Display(Name = "Duration")]
  public decimal Duration { get; set; }
  ...
}

My kendo Grid

@(Html.Kendo().Grid<JobPlanViewModel>()
      ...
      .Editable(editable => editable.Mode(GridEditMode.PopUp)
      .TemplateName("AdminJobPlanJobFormEditor")
      .Window(w => { w.Title(""); })
      .DisplayDeleteConfirmation(false))
      ...

      .Update(update => update.Action("JobPlanGrid_Update", "JobPlan", new { area = "AdminJobPlan" }))
)

AdminJobPlanJobFormEditor.cshtml

@model DefaultViewModel.JobPlanViewModel
@using Kendo.Mvc.UI
...
<div class="col-md-8 col-xs-12">
    @(Html.Kendo().NumericTextBoxFor(model => model.Duration)
    .Value(0.1m)
    .Step(0.1m)
    .Min(0.1m).Decimals(1).Format("N1")
    .HtmlAttributes(new { style = "width: 100%; padding: 0px;", required = "required" }))
    @Html.ValidationMessageFor(model => model.Duration)
</div>

I have a Kendo MVC Grid which uses popup edit mode, with a cshtml template.

In that template I've got a Kendo MVC NumerictextboxFor the duration field, which I want to set its default value to 0.1, and when user clicks "Save", this value should post to server side action JobPlanGrid_Update even he does not edit the duration field

In the UI, everything seems alright, when the edit window popup, the default value 0.1 is shown in the textbox, the min value, step value are alright, but when I click "Save", I found that the duration field posted is always 0 if I did not edit duration manually.

I have set the Grid's Datasource parameterMap. By writing console.log() , I found that when it is doing the mapping (before the grid sends a request to JobPlanGrid_Update ), the duration is already 0, which is out of my expectation.

What am I doing wrong and how can I achieve what I want to? Thanks!

EDITED

My full grid is like this:

@(Html.Kendo().Grid<JobPlanViewModel>()
    .Name("AdminJobPlanGrid")
    .AutoBind(false)
    .Columns(columns =>
    {
        columns.Bound(c => c.JobNo).Title(@Resources.Wording.JobNo).Width(80);
        columns.Bound(c => c.Description).Title(@Resources.Wording.Description);
        columns.Bound(c => c.Duration).Title(@Resources.Wording.DurationHours).ClientTemplate("#= kendo.format(\'{0:N1}\', Duration) #").Width(200);
        columns.Command(c => { c.Edit().UpdateText(Wording.Save).CancelText(Wording.Cancel).Text(Wording.Edit); c.Custom("Delete").Click("onAdminJobPlanJobDelete").Text(Wording.Delete);     }).Width(200);
    })
    .Pageable(page =>
    {
        page.Enabled(true);
        page.Messages(m =>
        {
            m.Display(Resources.Wording.GridDisplay);
            m.Empty(Resources.Wording.GridEmpty);
            m.Page(Resources.Wording.GridPage);
            m.Of(Resources.Wording.GridOf);
            m.ItemsPerPage(Resources.Wording.GridItemsPerPage);
            m.First(Resources.Wording.GridFirst);
            m.Previous(Resources.Wording.GridPrevious);
            m.Next(Resources.Wording.GridNext);
            m.Last(Resources.Wording.GridLast);
            m.Refresh(Resources.Wording.GridRefresh);
         });
    })
    .Scrollable(s => s.Height(80))
    .Sortable(s => s.Enabled(true))
    .Editable(editable => editable.Mode(GridEditMode.PopUp)
    .TemplateName("AdminJobPlanJobFormEditor")
    .Window(w => { w.Title(""); })
    .DisplayDeleteConfirmation(false))
    .DataSource(dataSource => dataSource.Ajax()
    .PageSize(20)
    .Sort(p => p.Add("JobNo").Ascending())
    .Model(model =>
    {
        model.Id(p => p.JobPlanJobID);
    })
    .Read(read => read.Action("JobPlanGrid_Read", "JobPlan", new { area = "AdminJobPlan" }))
    .Create(insert => insert.Action("JobPlanGrid_Update", "JobPlan", new { area = "AdminJobPlan" }))
    .Update(update => update.Action("JobPlanGrid_Update", "JobPlan", new { area = "AdminJobPlan" }))
    .Destroy(delete => delete.Action("JobPlanGrid_Update", "JobPlan", new { area = "AdminJobPlan" }))
    .ServerOperation(true))
)

Sorry for the formatting, I don't know why shift+tab will clear all the indent instead of clear one tab....

You haven't posted your grid completely, so I'll just have to assume it uses Ajax binding.

That's because of the way Kendo Grid works. I have had similar issues before. For example, if you try to change the value of an input on a popup editor using jquery, you'll see that the changed value will not be posted to your controller (the value will not change in the grid's data source), unless you explicitly trigger the change event of the input after changing its value.

Regardless of that, what you are doing is not the right way of setting a default value for a property in your grid. In your grid's data source, you should do this:

.DataSource(dataSource => dataSource
    .Ajax()
    .Model(m => m.Field(f => f.Duration).DefaultValue(0.1M))
    // other configurations...
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM