【问题标题】:How to set default value for any field of a ViewModel using Kendo UI Grid and ASP.NET MVC?如何使用 Kendo UI Grid 和 ASP.NET MVC 为 ViewModel 的任何字段设置默认值?
【发布时间】:2013-11-12 23:02:05
【问题描述】:

当我们使用 Kendo UI Grid 单击 Add New 或 Edit 按钮时,我们如何为任何模型字段设置默认值?

例如,我有两个视图模型 City 和 State。城市视图模型包含 StateID 作为城市模型的外键。

假设我有一个视图页面,在该页面中,我从州下拉列表中进行选择,以获取包含该州城市列表的 Kendo Grid。现在我想在该州内创建一个新城市,因此我需要将 StateID 值设置为下拉列表中的选定值。

如何实现此功能?

【问题讨论】:

    标签: asp.net-mvc grid kendo-ui


    【解决方案1】:

    好吧,我花了相当长的时间来解决上述问题,直到我偶然发现了这个link。而且解决方案非常简单。

    我需要做的就是添加“编辑”事件并指定将在编辑剑道 UI 网格时调用的 javascript 函数。然后我们可以在javascript函数上设置默认值。

    @(Html.Kendo().Grid()
                  .Events( e => e.Edit("onEdit") )
    )
    
    <script type="text/javascript">
    function onEdit(e) {
    
        var stateID = $("#hfStateID).val(); // I have set the dropdownlist selected value to  //the hidden field
    
        //check if record is new
        if (e.model.isNew()) {
            //set the default value for StateID
            e.model.set("StateID", stateID );
        }
    }
    </script>
    

    认为它可能会对某人有所帮助。

    谢谢。

    【讨论】:

      【解决方案2】:

      同意ajexpess。

      我还必须删除必需的 DataAnotation。

      例如,从您尝试设置的属性中删除:[Required(ErrorMessage = "Value should not be empty")]。

      【讨论】:

        猜你喜欢
        • 2012-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-27
        相关资源
        最近更新 更多