我在尝试使用asp.net mvc telerik grid提供的clientevents时遇到严重问题。 这是两种不同的场景: 当我将以下脚本放在视图或局部视图中时,它可以正常工作;
<%= Html.Telerik().Grid<Plan>()
.Name("Grid")
.DataKeys(keys => keys.Add(p => p.StaffId))
.ToolBar(commands => { commands.SubmitChanges();
commands.Position(GridToolBarPosition.Bottom);
}).PrefixUrlParameters(false)
.DataBinding(dataBinding =>
dataBinding.Ajax()
.Select("_SelectBatchEmployees", "Employee")
.Update("_SaveBatchEmployees", "Employee")
)
.Columns(columns =>
{
columns.Bound(p => p.FullName).Width(320).ReadOnly(true);
columns.Bound(p => p.Title).Width(220).ReadOnly(true);
columns.Bound(p => p.PerformanceRating).Title("Perf. Rating").Format("{0:n}").Width(80);
columns.Bound(p => p.RecommendedIncreaseAmountFrom).Width(80);
})
.ClientEvents(events =>
events.OnDataBinding("Grid_onDataBinding").OnError("Grid_onError").OnEdit("onEdit").OnDataBound("onDataBound"))
.Editable(editing => editing.Mode(GridEditMode.InCell))
.Pageable(page => page.PageSize(40))
.Scrollable()
.Sortable()
.Footer(true)
%>
<script type="text/javascript">
function Grid_onError(args) {
if (args.textStatus == "modelstateerror" && args.modelState) {
var message = "Errors:\n";
$.each(args.modelState, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
args.preventDefault();
alert(message);
}
}
function onDataBound(e) {
//alert(e.dataItem);
}
function onEdit(e) {
var dataItem = e.dataItem;
var mode = e.mode;
var form = e.form;
var priceTextBox = $(form).find('#Grade');
dataItem.RecommendedIncreaseAmountFrom = 10 * 5;
e.preventDefault();
}
function Grid_onDataBinding(e) {
var grid = $(this).data('tGrid');
if (grid.hasChanges()) {
if (!confirm('You are going to lose any unsaved changes. Are you sure?')) {
e.preventDefault();
}
}
}
</script>
但是,当我按需加载部分加载视图时,它不起作用。当我删除clientevents部分它开始工作?我不知道是什么原因,但我需要工作,请帮助我,这是我的代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Edit.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%=
Html.Telerik().TabStrip()
.Name("planingTabs").HtmlAttributes(new {style = "overfloat:auto"})
.Items(parent => parent.Add() //.HtmlAttributes(new {style = "overfloat:auto"})
.Text("Test")
.LoadContentFrom("PlanGrid", "Employee").Selected(true))
%>
</asp:Content>
以下是控制器代码:
public ActionResult Index()
{
return View();
}
public ActionResult PlanGrid()
{
return PartialView();
}
[GridAction]
public ActionResult _SelectBatchEmployees()
{
return View(new GridModel { Data = _employeeRepository.GetAll()});
}
[AcceptVerbs(HttpVerbs.Post)]
[CultureAwareAction]
[GridAction]
public ActionResult _SaveBatchEmployees([Bind(Prefix =
"inserted")]IEnumerable<Plan> insertedProducts,
[Bind(Prefix = "updated")]IEnumerable<Plan> updatedProducts,
[Bind(Prefix = "deleted")]IEnumerable<Plan> deletedProducts)
{
//if (insertedProducts != null)
//{
// foreach (var product in insertedProducts)
// {
// SessionProductRepository.Insert(product);
// }
//}
if (updatedProducts != null)
{
foreach (var employee in updatedProducts)
{
var target = _employeeRepository.One(p => p.StaffId == employee.StaffId);
if (target != null)
{
//target.ProductName = product.ProductName;
//target.UnitPrice = product.UnitPrice;
//target.UnitsInStock = product.UnitsInStock;
//target.LastSupply = product.LastSupply;
//target.Discontinued = product.Discontinued;
_employeeRespository.Save();
}
}
}
//if (deletedProducts != null)
//{
// foreach (var product in deletedProducts)
// {
// SessionProductRepository.Delete(product);
// }
//}
return View(new GridModel { Data = _employeeRespository.GetAll() });
}
答案 0 :(得分:0)
这是因为未加载Grid的JavaScript文件。 troubleshooting help topic解释了这一点。解决方案是手动注册JavaScript files。有一个工作项目展示了如何执行此操作here。