我必须创建一个 nopCommerce 插件来实现一些带有下拉列表的 Telerik 网格作为可编辑的行,问题是该插件是一个类库,而不是 ASP.NET MVC 应用程序,并且路由是在 RouteProvider.cs 文件中手动完成的. 所以我按照这里的例子:http: //demos.telerik.com/aspnet-mvc/razor/grid/clientedittemplates 并且必须具有以下配置:我的模型:
public class ContentModel : BaseNopModel
{
public string ContentId { get; set; }
[NopResourceDisplayName("Plugins.Widgets.DiscountBanner.Title")]
public string Title { get; set; }
[NopResourceDisplayName("Plugins.Widgets.DiscountBanner.Content")]
public string Content { get; set; }
[UIHint("DiscountsEditor"), Required]
public string Discounts { get; set; }
}
我的控制器:
[GridAction]
public ActionResult _SelectAjaxEditing()
{
List<ContentModel> model = new List<ContentModel>();
return View("Nop.Plugin.Widgets.DiscountBanner.Views.WidgetsDiscountBanner.Configure", _discountBannerSettings.contentGrid == "" || _discountBannerSettings.contentGrid == null ? new GridModel(model) : new GridModel(JsonHelper.JsonDeserialize<List<ContentModel>>(_discountBannerSettings.contentGrid)));
}
我的观点:
@{
Layout = "";
}
@model Nop.Plugin.Widgets.DiscountBanner.Models.ConfigurationModel
@using Nop.Plugin.Widgets.DiscountBanner.Models;
@using Nop.Web.Framework;
@using Telerik.Web.Mvc.UI;
@using Nop.Services.Discounts;
<script type="text/javascript">
function onEdit(e) {
$(e.form).find('#Discounts').data('tDropDownList').select(function (dataItem) {
return dataItem.Value == e.dataItem['Text'];
});
}
</script>
<table class="adminContent">
<tr>
<td class="adminTitle" colspan="2">
DiscountBanner Plugin for sing-up and sales tracking script implementation on your site
</td>
</tr>
<tr>
<td class="adminTitle">
@Html.NopLabelFor(model => model.ZoneId):
</td>
<td class="adminData">
@Html.DropDownListFor(model => model.ZoneId, Model.AvailableZones)
@Html.ValidationMessageFor(model => model.ZoneId)
</td>
</tr>
<tr>
<td class="adminTitle" colspan="2">
@(Html.Telerik().Grid<ContentModel>()
.DataKeys(keys =>
{
keys.Add(model => model.ContentId);
})
.Name("discount-settings-grid")
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.DataBinding(dataBinding =>
{
dataBinding.Ajax()
.Select("_SelectAjaxEditing", "WidgetsDiscountBanner")
.Insert("_InsertAjaxEditing", "WidgetsDiscountBanner")
.Update("_SaveAjaxEditing", "WidgetsDiscountBanner")
.Delete("_DeleteAjaxEditing", "WidgetsDiscountBanner");
}
)
.Columns(columns =>
{
columns.Bound(c => c.Title);
columns.Bound(c => c.Content);
columns.Bound(c => c.Discounts);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
commands.Delete().ButtonType(GridButtonType.Text);
}).Width(180).Title("Commands");
})
.ClientEvents(events => events.OnEdit("onEdit"))
.EnableCustomBinding(true)
)
</td>
</tr>
</table>
而我的自定义视图“DiscountsEditor.cshtml”:
@using Telerik.Web.Mvc.UI;
@using Nop.Services.Discounts;
@{
var _discountService = Nop.Core.Infrastructure.EngineContext.Current.Resolve<IDiscountService>();
var discounts = _discountService.GetAllDiscounts(null, true);
List<SelectListItem> listDiscounts = new List<SelectListItem>();
foreach (var item in discounts)
{
listDiscounts.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
}
}
@Html.Telerik().DropDownList().Name("Discounts").BindTo(listDiscounts)
据我所知,telerik 没有找到我的部分视图 DiscountsEditor.cshtml,因为该项目是一个类库并且路由值不一样,对于特定项目,我必须将我的视图设为“嵌入式资源”,这样我就可以用全名调用它们,问题是我无法将 Telerik Grid 配置为使用其他地方的局部视图。
我尝试使用这样的自定义模板:
columns.Bound(c => c.Discounts)
.ClientTemplate(
Html.Telerik().DropDownList().Name("Discount").BindTo(listDiscounts).ToHtmlString());
但是模板在我将一些信息放入网格后加载并且不可编辑,如果我想编辑它,下拉列表将转换为文本框,我不知道下一步该做什么,或者我是否可以使用另一个控制器但要加载当前视图的下拉列表不是部分视图。
谢谢。