【问题标题】:How to bind model value to kendo.ui.Slider?如何将模型值绑定到 kendo.ui.Slider?
【发布时间】:2023-03-13 20:43:01
【问题描述】:

我在我的 MVC 应用程序中使用 kendo.ui.Slider,如下所示:

@(Html.Kendo().Slider()
                .Name("slider") //The name of the slider is mandatory. It specifies the "id" attribute of the widget.
                .Min(0) //Set min value of the slider
                .Max(100000) //Set min value of the slider
                .Value(50000) //Set the value of the slider
                .SmallStep(1000)
                .LargeStep(10000)
                .HtmlAttributes(new { style = "width:700px;" })
                        .Tooltip(tooltip =>
                        {
                            tooltip.Format("{0:n0}");
                        })
                .Events(e =>
                        {
                            e.Change("sliderOnChange");
                        })
            )
            <script>
                function sliderOnChange(e) {
                    var slider = $("#slider").data("kendoSlider");
                    var sliderValue = slider.value();
                    alert(sliderValue);
                }
            </script>

如何绑定模型值而不是在这里分配静态值 (.Value(50000))?

【问题讨论】:

    标签: javascript asp.net-mvc asp.net-mvc-4 kendo-ui slider


    【解决方案1】:

    型号

    如果你有如下模型:

    public class AModel
    {
        public int? Percentage { get; set; }
    }
    

    以上内容允许在您针对模型的操作中设置属性。

    然后您可以使用SliderFor 而不是Slider

    查看

    @(Html.Kendo().SliderFor(m => m.Percentage)
                    .Min(0) //Set min value of the slider
                    .Max(100000) //Set min value of the slider
                    .SmallStep(1000)
                    .LargeStep(10000)
                    .HtmlAttributes(new { style = "width:700px;" })
                            .Tooltip(tooltip =>
                            {
                                tooltip.Format("{0:n0}");
                            })
                    .Events(e =>
                            {
                                e.Change("sliderOnChange");
                            })
                )
    

    请注意上面删除的 Name 属性和 Value 设置器。

    由于Percentage 属性在您的视图模型中定义,它会自动呈现为滑块的Name 属性。

    这允许模型绑定器拾取它并将设置值发布到您的模型对象中。

    【讨论】:

    • 是否可以将模型值动态绑定到 Max & Min?
    • @giparekh 是的,如果您创建另一个问题并将其添加到此处的 cmets,我会为您解答。 :)
    • 嗨 hutchonoid,我为我的确切要求创建了另一个问题。请检查一下。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2016-12-06
    • 1970-01-01
    相关资源
    最近更新 更多