C# &引用;参数字典包含参数“0”的空条目如何修复?
我试图实现一个编辑页面,以便管理员修改数据库中的数据。不幸的是,我遇到了一个错误 代码如下:C# &引用;参数字典包含参数“0”的空条目如何修复?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我试图实现一个编辑页面,以便管理员修改数据库中的数据。不幸的是,我遇到了一个错误 代码如下: public ViewResult Edit(int productId) { // Do something here } 但我得到了这个错误: "The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32' for metho
public ViewResult Edit(int productId) {
// Do something here
}
但我得到了这个错误:
"The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Edit(Int32)' in 'WebUI.Controllers.AdminController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters"
我在Global.asax.cs
中更改了路线,如下所示:
routes.MapRoute(
"Admin",
"Admin/{action}/{ productId}",
new { controller = "Admin", action = "Edit", productId= "" }
);
但是我仍然得到了错误。框架将把
productId
的空字符串(在您的默认路径中)解析为空条目,并且由于int
不允许null
…您得到了错误
更改:
public ViewResult Edit(int productId)
到
如果您希望允许调用者不必传入产品id,则根据路由的配置方式,您希望这样做
您还可以重新配置默认路由,以便在未提供productId时传入一些已知的默认路由:
routes.MapRoute(
"Admin",
"Admin/{action}/{ productId}",
new { controller = "Admin", action = "Edit", productId= -1 }
标准的“int”类(int32)不接受null值,在这种情况下,它将失败从空字符串到int的转换,并尝试为其分配null
我可能会看看您试图实现的目标--如果您试图强制管理员提供productID,以便他们编辑数据库中的记录,我会考虑将其放入请求对象或其他一些方法中,以提供更多的通用性。
productId
应限制为int
类型
new {controller="Admin", action="Edit"},
new {productId = @"\d+" }
可能您忘记在视图中传递所需的数据(在本例中为“productId”)。
我假设您试图通过单击索引页面中的链接来访问详细信息,我也假设该链接为“View\Admin\index.cshtml”
@ActionLink(“编辑”,“编辑”,新的{productId=item.productId})|
@ActionLink(“Details”,“Details”,new{productId=item.productId})|//注意productId由item.productId填充
@ActionLink(“删除”,“删除”,新的{productId=item.productId})
如果不这样做,将导致所有参数都为null,从而导致错误。以下是如何为任何控制器方法调用忽略此类参数错误的方法:
public class MyControllerBase
{
//...
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Exception != null)
{
var targetSite = filterContext.Exception.TargetSite;
if (targetSite.DeclaringType != null)
if (targetSite.DeclaringType.FullName == typeof(ActionDescriptor).FullName)
if (targetSite.Name == "ExtractParameterFromDictionary") // Note: may be changed in future MVC versions
{
filterContext.ExceptionHandled = true;
filterContext.Result = new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
return;
}
//...
}
// ...
}
}
在Pro ASP.Net中运行的SportStore示例之后,我遇到了相同的问题 解决方案实际上是我的索引视图有以下代码
@Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
但是,我的控制器中的编辑方法被定义为
public ViewResult Edit(int productId)
将我的索引视图更改为
@Html.ActionLink("Edit", "Edit", new { productId=item.ProductID }) |
修复了该问题在路由上更改它可能会涉及使用相同url的其他路由。保持简单,覆盖所有基础
[HttpGet]
public ActionResult ThePage(int id = -1)
{
if(id == -1)
{
return RedirectToAction("Index");
}
//Or proceed as normal
}
如果这是您访问页面时遇到的错误,因为您需要一个id(例如,人们在不应该的情况下将url放入地址栏),那么这也很好,然后将参数设置为可选值
编辑:对不起int,问题问的时间不长
@Html.ActionLink("Edit", "Edit", new { productId=item.ProductID }) |
[HttpGet]
public ActionResult ThePage(int id = -1)
{
if(id == -1)
{
return RedirectToAction("Index");
}
//Or proceed as normal
}