Kendo MVC NumericTextBoxFor 默认值未绑定到模型
Kendo MVC NumericTextBoxFor default value not binding to model
JobPlanViewModel
public class JobPlanViewModel : IMapFrom<JobPlan>, IJobPlan
{
...
[Display(Name = "Duration")]
public decimal Duration { get; set; }
...
}
我的kendo网格
@(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>
我有一个 Kendo MVC 网格,它使用弹出编辑模式,带有一个 cshtml 模板。
在该模板中,我有一个 Kendo MVC NumerictextboxFor duration
字段,我想将其默认值设置为 0.1,当用户单击 "Save" 时,这值应该 post 到服务器端操作 JobPlanGrid_Update
即使他不编辑 duration
字段
在UI里一切正常,编辑window弹窗时,文本框显示默认值0.1,最小值,步长值都可以,但是当我点击"Save",我发现如果我没有手动编辑duration
,duration
字段posted总是0。
我已经设置了网格的数据源参数映射。通过写console.log()
,我发现它在做映射的时候(在grid向JobPlanGrid_Update
发送请求之前),duration
已经是0了,这出乎我的意料。
我做错了什么,我该如何实现我想要的?谢谢!
已编辑
我的全格子是这样的:
@(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))
)
抱歉格式化,我不知道为什么 shift+tab 会清除所有缩进而不是清除一个制表符....
您还没有完全发布您的网格,所以我只能假设它使用 Ajax 绑定。
这是因为 Kendo 网格的工作方式。我以前遇到过类似的问题。例如,如果您尝试使用 jquery 更改弹出编辑器上的输入值,您将看到更改后的值不会发布到您的控制器(网格数据源中的值不会更改) ), 除非您在更改其值后显式触发输入的更改事件。
无论如何,您所做的并不是在网格中为 属性 设置默认值的正确方法。在您的网格数据源中,您应该这样做:
.DataSource(dataSource => dataSource
.Ajax()
.Model(m => m.Field(f => f.Duration).DefaultValue(0.1M))
// other configurations...
)
JobPlanViewModel
public class JobPlanViewModel : IMapFrom<JobPlan>, IJobPlan
{
...
[Display(Name = "Duration")]
public decimal Duration { get; set; }
...
}
我的kendo网格
@(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>
我有一个 Kendo MVC 网格,它使用弹出编辑模式,带有一个 cshtml 模板。
在该模板中,我有一个 Kendo MVC NumerictextboxFor duration
字段,我想将其默认值设置为 0.1,当用户单击 "Save" 时,这值应该 post 到服务器端操作 JobPlanGrid_Update
即使他不编辑 duration
字段
在UI里一切正常,编辑window弹窗时,文本框显示默认值0.1,最小值,步长值都可以,但是当我点击"Save",我发现如果我没有手动编辑duration
,duration
字段posted总是0。
我已经设置了网格的数据源参数映射。通过写console.log()
,我发现它在做映射的时候(在grid向JobPlanGrid_Update
发送请求之前),duration
已经是0了,这出乎我的意料。
我做错了什么,我该如何实现我想要的?谢谢!
已编辑
我的全格子是这样的:
@(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))
)
抱歉格式化,我不知道为什么 shift+tab 会清除所有缩进而不是清除一个制表符....
您还没有完全发布您的网格,所以我只能假设它使用 Ajax 绑定。
这是因为 Kendo 网格的工作方式。我以前遇到过类似的问题。例如,如果您尝试使用 jquery 更改弹出编辑器上的输入值,您将看到更改后的值不会发布到您的控制器(网格数据源中的值不会更改) ), 除非您在更改其值后显式触发输入的更改事件。
无论如何,您所做的并不是在网格中为 属性 设置默认值的正确方法。在您的网格数据源中,您应该这样做:
.DataSource(dataSource => dataSource
.Ajax()
.Model(m => m.Field(f => f.Duration).DefaultValue(0.1M))
// other configurations...
)