Kendo Grid - 如何在编辑模式下禁用级联下拉列表,并在添加模式时启用它?

时间:2015-03-19 18:06:27

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

我想在编辑记录时禁用编辑器中另一个下拉列表级联的DropDownListFor,并在添加记录时保持启用。我在我的MVC项目中使用了Kendo Grid。

这里我想在编辑模式下禁用国家/地区下拉列表和位置下拉列表,并在添加(插入新记录)模式时保持启用

代码如下:

网格:

@(Html.Kendo().Grid<Webapplication1.Models.mainViewModel>()
    .Name("mainGrid")
    .Columns(c =>
    {
        c.Bound(m => m.Id).Hidden();
        c.Bound(m => m.CountryViewModel.CountryName)
            .Title("Country").HeaderHtmlAttributes(new { @title = "Countries" });
        c.Bound(m => m.LocationViewModel.LocationName)
            .Title("Location").HeaderHtmlAttributes(new { @title = "Locations" });
        c.Command(p =>
        {p.Edit().Text(" ").UpdateText(" ").CancelText(" ").HtmlAttributes(new { @title = "Edit" });});
    })
    .ToolBar(toolbar => { toolbar.Create().Text("").HtmlAttributes(new { @title = "Add"}); })
    .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.PopUp).TemplateName("gridEditor"))
    .DataSource(dataSource => dataSource
.Ajax()
        .PageSize(10)
        .Read(read => read.Action("mainGrid_Read", "abc"))
    .Update(update => update.Action("mainGrid_Update", "abc"))
    .Create(create => create.Action("mainGrid_Create", "abc"))
        .Model(m => { m.Id(p => p.Id);
          m.Field(p => p.CountryViewModel).DefaultValue(ViewData["DefaultCountry"] as Webapplication1.Models.CountryViewModel);      
        })
    )
)

国家DropDownList:

@model Webapplication1.Models.CountryViewModel

@(Html.Kendo().DropDownListFor(m => m)
    .AutoBind(false)
    .OptionLabel("Select")
    .DataValueField("CountryId")
    .DataTextField("CountryName") 
    .DataSource(dataSource =>
                  {
                      dataSource.Read(read => read.Action("GetCountries", "abc").Type(HttpVerbs.Post))
                                .ServerFiltering(true);
                  })
)
@Html.ValidationMessageFor(m => m)

位置DropDownList:

@model Webapplication1.Models.LocationViewModel

@(Html.Kendo().DropDownListFor(m => m)
    .OptionLabel("Select")
    .DataValueField("LocationId")
    .DataTextField("LocationName")
    .AutoBind(false)
    .DataSource(d =>
    {
        d.Read(read =>
        {
            read.Action("GetLocationsByCountryId", "abc").Type(HttpVerbs.Post).Data("getCountryId");
        }).ServerFiltering(true);
    })
    .CascadeFrom("CountryViewModel")
)
@Html.ValidationMessageFor(m => m)

弹出编辑器:

@model Webapplication1.Models.mainViewModel
<table>
    @Html.HiddenFor(m => m.Id)
    <tr>
        <td>@Html.Label("Country:")</td>
        <td>@Html.EditorFor(m => m.CountryViewModel)</td>
    </tr>
    <tr>
        <td>@Html.Label("Location:")</td>
        <td>@Html.EditorFor(m => m.LocationViewModel)</td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:0)

将Edit事件添加到您的网格中,如下所示:

@(Html.Kendo().Grid<Webapplication1.Models.mainViewModel>()
    .Name("mainGrid")
    .Columns(c =>
    {
        c.Bound(m => m.Id).Hidden();
        c.Bound(m => m.CountryViewModel.CountryName)
            .Title("Country").HeaderHtmlAttributes(new { @title = "Countries" });
        c.Bound(m => m.LocationViewModel.LocationName)
            .Title("Location").HeaderHtmlAttributes(new { @title = "Locations" });
        c.Command(p =>
        {p.Edit().Text(" ").UpdateText(" ").CancelText(" ").HtmlAttributes(new { @title = "Edit" });});
    })
    .ToolBar(toolbar => { toolbar.Create().Text("").HtmlAttributes(new { @title = "Add"}); })
    .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.PopUp).TemplateName("gridEditor"))
    .DataSource(dataSource => dataSource
.Ajax()
        .PageSize(10)
        .Read(read => read.Action("mainGrid_Read", "abc"))
    .Update(update => update.Action("mainGrid_Update", "abc"))
    .Create(create => create.Action("mainGrid_Create", "abc"))
        .Model(m => { m.Id(p => p.Id);
          m.Field(p => p.CountryViewModel).DefaultValue(ViewData["DefaultCountry"] as Webapplication1.Models.CountryViewModel);      
        })
    )
    .Events(events => events.Edit("onEdit"))
)

然后,将此脚本添加到您的视图中:

<script type="text/javascript">
    function onEdit(e) {
        //if current model is new (add)...
        if (e.model.isNew()) {            
            e.container.find("#CountryViewModel").data("kendoDropDownList").enable();            
        }
        else
        {
            e.container.find("#CountryViewModel").data("kendoDropDownList").enable(false);            
        }

        e.preventDefault();
    }
</script>

顺便说一句,您不必为Country和Location创建ViewModels。您可以使用IEnumerable<SelectListItem>创建下拉列表。此外,无需创建EditorTemplates。将您的下拉列表直接放在视图中。

<强>更新

确保默认情况下禁用“位置”下拉列表。然后,您必须启用/禁用“国家/地区”下拉列表。

@(Html.Kendo().DropDownListFor(m => m)
    .OptionLabel("Select")
    .DataValueField("LocationId")
    .DataTextField("LocationName")
    .AutoBind(false)
    .DataSource(d =>
    {
        d.Read(read =>
        {
            read.Action("GetLocationsByCountryId", "abc").Type(HttpVerbs.Post).Data("getCountryId");
        }).ServerFiltering(true);
    })
    .CascadeFrom("CountryViewModel")
    .Enable(false)
)