【问题标题】:Kendo Grid - How to disable cascaded dropdownlistfor when in edit mode and enable it when add mode?Kendo Grid - 如何在编辑模式下禁用级联下拉列表并在添加模式下启用它?
【发布时间】:2015-03-19 18:06:27
【问题描述】:

我想在编辑记录时禁用从编辑器中另一个下拉列表级联的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);      
        })
    )
)

国家下拉列表:

@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)

位置下拉列表:

@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>

【问题讨论】:

  • 顺便说一句,你的代码看起来很奇怪。另一个下拉列表在哪里?您的 ViewModel 中还有什么?你怎么用DropDownListFor(m =&gt; m)这样的下拉列表?这是里面和EditorTemplate吗?
  • @ataravati:是的下拉列表在弹出编辑器中。我认为如果我将编辑模式更改为内联,它应该没有任何区别。我已经在这里编辑了代码并发布了网格、下拉列表编辑器和弹出编辑器代码。
  • 我从未说过将编辑模式更改为内联。似乎没有必要为 Country 和 Location 设置 EditorTemplates。

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


【解决方案1】:

像这样向您的网格添加一个编辑事件:

@(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 创建 ViewModel。您可以使用IEnumerable&lt;SelectListItem&gt; 创建下拉列表。此外,无需创建 EditorTemplates。将您的下拉列表直接放在您的视图中。

更新:

确保您的位置下拉列表默认处于禁用状态。然后,您必须启用/禁用 Country 下拉列表。

@(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)
)

【讨论】:

  • 这不起作用。它仅适用于国家下拉列表,不适用于位置下拉列表。我仍然可以更改位置下拉列表中的值。
  • 那个人怎么可能?只需查看源代码,看看位置下拉列表的id 是否与脚本中的匹配。如果它适用于一个,它也应该适用于另一个。是一样的。
  • 这里的 id 是相同的“LocationViewModel”。我认为这是因为 .CascadeFrom("CountryViewModel") 。我的意思是位置下拉列表是从国家下拉列表级联的,这就是它应该以不同方式处理的原因。
  • 试试我更新的答案,看看它是否有效。请注意,脚本也已更新。
  • 哦,对不起!我刚刚意识到您有 Country DropDownList 的默认值。这会自动启用级联 DropDownList。删除默认值,看看它是否有效。
猜你喜欢
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 2017-11-26
  • 2015-09-13
  • 1970-01-01
相关资源
最近更新 更多