我正在尝试使用 dev express 网格进行内联编辑。我已按照 http://mvc.devexpress.com/GridView/InlineEditingWithTemplate中所述的示例进行操作, 并通过查看http://www.youtube.com/watch?v=dKCFLyvf32Y此链接进行了确认。我的控制器的代码如下:
[ValidateInput(false)]
public ActionResult InlineEditingWithTemplatePartial()
{
int siteID = (int)Session["siteID"];
return PartialView("InlineEditingWithTemplatePartial", employeeRepo.GetAllEmployeesForSite(siteID));
}
[HttpPost, ValidateInput(false)]
public ActionResult InlineEditingWithTemplateAddNewPartial([ModelBinder(typeof(DevExpressEditorsBinder))] Employee employee)
{
int siteID = 0;
if (ModelState.IsValid)
{
try
{
siteID = (int)Session["siteID"];
//employee.InsertProduct(employee);
}
catch (Exception e)
{
ViewData["EditError"] = e.Message;
}
}
else
{
ViewData["EditError"] = "Please, correct all errors.";
ViewData["EmployeeDetail"] = employee;
}
return PartialView("InlineEditingWithTemplatePartial", employeeRepo.GetAllEmployeesForSite(siteID));
}
[HttpPost, ValidateInput(false)]
public ActionResult InlineEditingWithTemplateUpdatePartial([ModelBinder(typeof(DevExpressEditorsBinder))] Employee employee)
{
int siteID = 0;
if (ModelState.IsValid)
{
try
{
siteID = (int)Session["siteID"];
employeeRepo.SaveEmployee(employee);
}
catch (Exception e)
{
ViewData["EditError"] = e.Message;
}
}
else
{
ViewData["EditError"] = "Please, correct all errors.";
ViewData["EnployeeDetail"] = employee;
}
return PartialView("InlineEditingWithTemplatePartial", employeeRepo.GetAllEmployeesForSite(siteID));
}
[HttpPost, ValidateInput(false)]
public ActionResult InlineEditingWithTemplateDeletePartial(Employee employee)
{
int siteID = 0;
if (employee!=null)
{
try
{
siteID = (int)Session["siteID"];
employeeRepo.DeleteEmployee(employee);
}
catch (Exception e)
{
ViewData["EditError"] = e.Message;
}
}
return PartialView("InlineEditingWithTemplatePartial", employeeRepo.GetAllEmployeesForSite(siteID));
}
视图的代码是:
@using MyProject6.Data.Models;
@using System.Web.UI.WebControls;
@using DevExpress.Web.Mvc.UI;
@using DevExpress.Web.ASPxEditors;
@using DevExpress.Web.Mvc;
@using DevExpress.Web.ASPxGridView;
@{
var grid = Html.DevExpress().GridView(
settings =>
{
settings.Name = "gvEmployeeEditing";
settings.KeyFieldName = "EmployeeID";
settings.CallbackRouteValues = new { Controller = "Home", Action = "InlineEditingWithTemplatePartial" };
settings.ClientSideEvents.BeginCallback = "function(s,e){e.customArgs['id'] = '" + ViewContext.RouteData.Values["EmployeeID"] + "'}";
settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Home", Action = "" }; //InlineEditingUpdatePartial
settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Home", Action = "InlineEditingWithTemplateUpdatePartial"};
settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Home", Action = "" }; //InlineEditingUpdatePartial
settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
settings.SettingsBehavior.ConfirmDelete = true;
settings.CommandColumn.Visible = true;
settings.CommandColumn.NewButton.Visible = true;
settings.CommandColumn.DeleteButton.Visible = true;
settings.CommandColumn.EditButton.Visible = true;
settings.Columns.Add("EmployeeID");
settings.Columns.Add("EmployeeCode");
settings.Columns.Add("FirstName");
settings.Columns.Add("LastName").PropertiesEdit.DisplayFormatString = "c";
settings.Columns.Add("DefaultRoleTitle");
settings.SetEditFormTemplateContent(c => {
var employee = ViewData["EditableEmployeeDetail"] != null ? ViewData["EditableEmployeeDetail"] : c.DataItem;
ViewContext.Writer.Write(
"<div class=\"edit_form\">" +
"<div class=\"line\">"
);
Html.DevExpress().Label(
edtSettings => {
edtSettings.ControlStyle.CssClass = "label";
edtSettings.Text = "Employee Code";
edtSettings.AssociatedControlName = "EmployeeCode";
}
)
.Render();
Html.DevExpress().TextBox(
edtSettings => {
edtSettings.Name = "EmployeeCode";
edtSettings.ControlStyle.CssClass = "editor";
edtSettings.ShowModelErrors = true;
}
)
.Bind(DataBinder.Eval(employee, "EmployeeCode"))
.Render();
ViewContext.Writer.Write(
"</div>" +
"<div class=\"line\">"
);
Html.DevExpress().Label(
edtSettings => {
edtSettings.ControlStyle.CssClass = "label";
edtSettings.Text = "First Name:";
edtSettings.AssociatedControlName = "FirstName";
}
)
.Render();
Html.DevExpress().TextBox(
edtSettings =>
{
edtSettings.Name = "FirstName";
edtSettings.ControlStyle.CssClass = "editor";
edtSettings.Text = "First Name";
edtSettings.ShowModelErrors = true;
}
).Bind(DataBinder.Eval(employee, "FirstName"))
.Render();
ViewContext.Writer.Write(
"</div>" +
"<div class=\"line\">"
);
Html.DevExpress().Label(
edtSettings => {
edtSettings.ControlStyle.CssClass = "label";
edtSettings.Text = "Last Name:";
edtSettings.AssociatedControlName = "LastName";
}
)
.Render();
Html.DevExpress().TextBox(
edtSettings => {
edtSettings.Name = "LastName";
edtSettings.ControlStyle.CssClass = "editor";
edtSettings.ShowModelErrors = true;
}
)
.Bind(DataBinder.Eval(employee, "LastName"))
.Render();
ViewContext.Writer.Write(
"</div>" +
"<div class=\"line\">"
);
Html.DevExpress().Label(
edtSettings =>
{
edtSettings.ControlStyle.CssClass = "label";
}
)
.Render();
Html.DevExpress().Button(
btnSettings =>
{
btnSettings.Name = "btnUpdate";
btnSettings.ControlStyle.CssClass = "button";
btnSettings.Text = "Update";
btnSettings.ClientSideEvents.Click = "function(s, e){ gvEmployeeEditing.UpdateEdit(); }";
}
)
.Render();
Html.DevExpress().Button(
btnSettings =>
{
btnSettings.Name = "btnCancel";
btnSettings.ControlStyle.CssClass = "button";
btnSettings.Text = "Cancel";
btnSettings.ClientSideEvents.Click = "function(s, e){ gvEmployeeEditing.CancelEdit(); }";
}
)
.Render();
ViewContext.Writer.Write(
"</div></div>"
);
});
settings.PreRender = (sender, e) =>
{
MVCxGridView gridView = (MVCxGridView)sender;
};
});
if(ViewData["EditError"] != null){
grid.SetEditErrorText((string)ViewData["EditError"]);
}
}
@grid.Bind(Model).GetHtml()
这个视图是我从 Home Controller 的索引视图中调用的部分视图。我试图调试它,只是没有输入内联编辑的代码。看起来它没有回调任何东西。我不确定我在这里缺少什么,我在过去两天一直在寻找解决方案。