gpt4 book ai didi

javascript - Kendo MVC NumericTextBoxFor 默认值未绑定(bind)到模型

转载 作者:行者123 更新时间:2023-11-29 15:27:55 24 4
gpt4 key购买 nike

JobPlanViewModel

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

我的剑道格

@(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,当用户单击“保存”时,该值应该发布到服务器端操作 JobPlanGrid_Update,即使他没有编辑 duration 字段

在UI中,一切似乎都很好,弹出编辑窗口时,文本框中显示默认值0.1,最小值,步长值都可以,但是当我点击“保存”时,我发现duration字段如果我没有手动编辑 duration,则 posted 始终为 0。

我已经设置了网格的数据源参数映射。通过写入 console.log() ,我发现它在做映射时(在网格向 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 绑定(bind)。

这是因为 Kendo Grid 的工作方式。我以前遇到过类似的问题。例如,如果您尝试使用 jquery 更改弹出编辑器上的输入值,您将看到更改后的值不会发布到您的 Controller (该值不会在网格的数据源中更改),除非您在更改其值后显式触发输入的更改事件。

无论如何,您所做的并不是为网格中的属性设置默认值的正确方法。在您的网格数据源中,您应该这样做:

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

关于javascript - Kendo MVC NumericTextBoxFor 默认值未绑定(bind)到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37450978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com