ASP.NET MVC调用控制器方法DevExpress
ASP.NET MVC Calling Controller Method DevExpress
我试图在控制器上调用一个方法,但它给了我一个空错误。我试图在代码上执行 getKeyValue,但它不起作用。我不知道我做错了什么。感谢您的帮助。
控制器代码
public ActionResult EditRecord(int id)
{
int x = id;
return PartialView("~/Views/FileMaintenance/Principal/EditPrincipal.cshtml", PrincipalInfo);
}
DevExpress GridView 代码
settings.Columns.Add(column =>
{
column.FieldName = "Unbound";
column.Caption = "Action";
column.UnboundType = DevExpress.Data.UnboundColumnType.Object;
column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
column.ReadOnly = false;
column.ColumnType = MVCxGridViewColumnType.ButtonEdit;
column.SetDataItemTemplateContent((c) =>
{
Html.DevExpress().Button(b =>
{
b.Name = "btnVE" + c.KeyValue;
b.Text = "V/E";
b.UseSubmitBehavior = false; // prevent default submit action
b.EnableClientSideAPI = true; // add this line if not sure
b.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = '{0}?key={1}'; }}",
DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" }),
c.KeyValue.ToString());
}).GetHtml();
});
});
错误
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult EditRecord(Int32)' in 'WMS_Web.Controllers.FileMaintenance.ViewPrincipalController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
您没有在 DevExpressHelper.GetUrl
方法中提供参数 id
。你需要设置它。在下面的示例中,我将值设置为 1。我不明白为什么在要求的 "id" 时在查询字符串中放置 "key"。将 "key" 更改为 "id" 应该也可以解决您的问题。
settings.Columns.Add(column =>
{
column.FieldName = "Unbound";
column.Caption = "Action";
column.UnboundType = DevExpress.Data.UnboundColumnType.Object;
column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
column.ReadOnly = false;
column.ColumnType = MVCxGridViewColumnType.ButtonEdit;
column.SetDataItemTemplateContent((c) =>
{
Html.DevExpress().Button(b =>
{
b.Name = "btnVE" + c.KeyValue;
b.Text = "V/E";
b.UseSubmitBehavior = false; // prevent default submit action
b.EnableClientSideAPI = true; // add this line if not sure
b.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = '{0}'; }}",
DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord", id = c.KeyValue.ToString() }));
}).GetHtml();
});
});
我试图在控制器上调用一个方法,但它给了我一个空错误。我试图在代码上执行 getKeyValue,但它不起作用。我不知道我做错了什么。感谢您的帮助。
控制器代码
public ActionResult EditRecord(int id)
{
int x = id;
return PartialView("~/Views/FileMaintenance/Principal/EditPrincipal.cshtml", PrincipalInfo);
}
DevExpress GridView 代码
settings.Columns.Add(column =>
{
column.FieldName = "Unbound";
column.Caption = "Action";
column.UnboundType = DevExpress.Data.UnboundColumnType.Object;
column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
column.ReadOnly = false;
column.ColumnType = MVCxGridViewColumnType.ButtonEdit;
column.SetDataItemTemplateContent((c) =>
{
Html.DevExpress().Button(b =>
{
b.Name = "btnVE" + c.KeyValue;
b.Text = "V/E";
b.UseSubmitBehavior = false; // prevent default submit action
b.EnableClientSideAPI = true; // add this line if not sure
b.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = '{0}?key={1}'; }}",
DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" }),
c.KeyValue.ToString());
}).GetHtml();
});
});
错误
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult EditRecord(Int32)' in 'WMS_Web.Controllers.FileMaintenance.ViewPrincipalController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
您没有在 DevExpressHelper.GetUrl
方法中提供参数 id
。你需要设置它。在下面的示例中,我将值设置为 1。我不明白为什么在要求的 "id" 时在查询字符串中放置 "key"。将 "key" 更改为 "id" 应该也可以解决您的问题。
settings.Columns.Add(column =>
{
column.FieldName = "Unbound";
column.Caption = "Action";
column.UnboundType = DevExpress.Data.UnboundColumnType.Object;
column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
column.ReadOnly = false;
column.ColumnType = MVCxGridViewColumnType.ButtonEdit;
column.SetDataItemTemplateContent((c) =>
{
Html.DevExpress().Button(b =>
{
b.Name = "btnVE" + c.KeyValue;
b.Text = "V/E";
b.UseSubmitBehavior = false; // prevent default submit action
b.EnableClientSideAPI = true; // add this line if not sure
b.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = '{0}'; }}",
DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord", id = c.KeyValue.ToString() }));
}).GetHtml();
});
});