【发布时间】: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 => m)这样的下拉列表?这是里面和EditorTemplate吗? -
@ataravati:是的下拉列表在弹出编辑器中。我认为如果我将编辑模式更改为内联,它应该没有任何区别。我已经在这里编辑了代码并发布了网格、下拉列表编辑器和弹出编辑器代码。
-
我从未说过将编辑模式更改为内联。似乎没有必要为 Country 和 Location 设置 EditorTemplates。
标签: asp.net-mvc kendo-grid kendo-asp.net-mvc kendo-dropdown