如何将模型值绑定到kendo.ui.Slider?

时间:2014-09-19 09:06:10

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

我在我的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))?

1 个答案:

答案 0 :(得分:0)

<强>模型

如果您的模型如下:

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设置者。

由于{View}模型中定义了Percentage属性,它会自动呈现为Slider的Name属性。

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