如何使用Kendo UI Grid和ASP.NET MVC为ViewModel的任何字段设置默认值?

aje*_*ess 2 asp.net-mvc grid kendo-ui

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

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

让我们假设我有一个视图页面,我从州下拉列表中选择一个Kendo Grid,填充该状态的城市列表.现在我想在该状态下创建一个新城市,因此我需要将StateID值设置为下拉列表中的选定值.

我如何实现此功能?

aje*_*ess 5

好吧,我花了很长时间才弄清楚上面的问题,直到我偶然发现了这个链接.解决方案非常简单.

我需要做的就是添加"编辑"事件并指定将在编辑时为Kendo 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>
Run Code Online (Sandbox Code Playgroud)

认为它可能会帮助某人.

谢谢.