我已经实现了kendo网格,右键单击上下文菜单以及当用户尝试选择上下文菜单时触发的select函数。当我选择编辑上下文菜单时,我试图传递某些列的值。我在客户端获取id和国家/地区代码,但我的控制器只获取id。不确定为什么国家代码会变为空。
网格
@(Html.Kendo().Grid<CC.GRP.MCRequest.ViewModels.RequestViewModel>()
.Name("GridRequest")
.Columns(columns => {
columns.Bound(o => o.RequestID).Width("100px");
columns.Bound(o => o.CountryCode).Width("100px");
columns.Bound(o => o.SalesOffice).Width("100px");
columns.Bound(o => o.CustomerNumber).Width("120px");
columns.Bound(o => o.ProjectName).Width("120px");
columns.Bound(o => o.ProjectContent).Width("120px");
columns.Bound(o => o.ContractStartDate).Width("140px");
columns.Bound(o => o.Priority).Width("120px");
columns.Bound(o => o.NameOfResponsiblePerson).Width("170px");
columns.Bound(o => o.Status).Width("100px");
columns.Bound(o => o.CreatedBy).Width("110px");
columns.Bound(o => o.CreatedDate).Width("110px");
columns.Bound(o => o.ModifiedBy).Width("110px");
})
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
.Groupable()
.Selectable(selectable => selectable
.Mode(GridSelectionMode.Single)
.Type(GridSelectionType.Row))
.Events(events => events
.Change("onChange")
)
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(t => t.RequestID))
.Read(read => read.Action("Requests_Read", "Request"))
)
)
上下文菜单
@(Html.Kendo().ContextMenu()
.Name("RequestMenu")
.Target("#GridRequest")
.Filter("tr")
.Orientation(ContextMenuOrientation.Vertical)
.Animation(animation =>
{
animation.Open(open =>
{
open.Fade(FadeDirection.In);
open.Duration(500);
});
})
.Items(items =>
{
items.Add()
.Text("Edit").HtmlAttributes(new { @class = "contextMenuItem" });
items.Add()
.Text("Update Status").HtmlAttributes(new { @class = "contextMenuItem" });
})
.Events(e =>
{
e.Select("onSelect");
})
)
的Javascript
function onSelect(e) {
var grid = $("#GridTeam").data("kendoGrid");
var requestId;
switch ($(e.item).children(".k-link").text()) {
case "Edit":
var grid = $("#GridRequest").data("kendoGrid");
// var column = grid.columns[0];
var selectedBackup = grid.dataItem(grid.select());
requestId = selectedBackup.id;
code = selectedBackup.CountryCode;
window.location.href = '@Url.Action("EditRequest", "Request", new { id = "_id_", status="Edit", countryCode = "_code_" })'.replace('_id_', requestId).replace('_code_', code);
break;
case "Update Status":
var statusWindow = $("#window").data("kendoWindow");
var grid = $("#GridRequest").data("kendoGrid");
var selectedRow = grid.dataItem(grid.select());
id = selectedRow.id;
name = selectedRow.ProjectName;
code = selectedRow.CountryCode;
statusWindow.refresh({ data: { requestId: id, projectName: name, countryCode: code } }).center().open();
}
}
控制器
[Route("Request/AddRequest")]
[Route("Request/EditRequest/{id?}")]
public ActionResult NewRequest_Read(string id, string countryCode)
{
NewRequestViewModel newReqeustViewModel = new NewRequestViewModel();
if (!string.IsNullOrEmpty(id))
{
var result = requestRepository.GetRequesById(int.Parse(id));
return View("NewRequestView", (Mapper.Map<NewRequestViewModel>(result)));
}
return View("NewRequestView", newReqeustViewModel);
}
routedebugger输出
route.config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Request", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Message",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Message", action = "Authorize", id = UrlParameter.Optional }
);
}