I am trying to implement an edit page in order administrator to modify data in database.Unfortunately I am encountering an error.
The code below:
public ViewResult Edit(int productId) {
// Do something here
}
but I am getting this error:
"The parameters dictionary contains a null entry for parameter 'productId' of non-nullable开发者_JS百科 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"
I changed my route in Global.asax.cs
like this:
routes.MapRoute(
"Admin",
"Admin/{action}/{ productId}",
new { controller = "Admin", action = "Edit", productId= "" }
);
but still I am getting the error .
That empty string for productId
(in your default route) will get parsed to a null entry by the Framework, and since int
does not allow null
...you're getting the error.
Change:
public ViewResult Edit(int productId)
to
public ViewResult Edit(int? productId)
if you want to allow for the caller to not have to pass in a product id, which is what it looks like what you want to do based on the way your route is configured.
You could also re-configure your default route to pass in some known default for when no productId is supplied:
routes.MapRoute(
"Admin",
"Admin/{action}/{ productId}",
new { controller = "Admin", action = "Edit", productId= -1 }
I came across the same problem following the worked SportStore example in Pro ASP.Net
The solution was actually that my Index view has the following code.
@Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
However my Edit method in my controller was defined as
public ViewResult Edit(int productId)
changing my Index view to read
@Html.ActionLink("Edit", "Edit", new { productId=item.ProductID }) |
fixed the problem
Here is the way how to ignore such argument errors for any controller method invoke:
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;
}
//...
}
// ...
}
}
productId
should be constrained to int
type.
new {controller="Admin", action="Edit"},
new {productId = @"\d+" }
Perhaps you forgot to pass the required data (in this case the 'productId') in the View.
I assume you try to access the detail by clicking the link in the index page which I also assumed it as "View\Admin\index.cshtml"
<td>
@Html.ActionLink("Edit", "Edit", new { productId = item.ProductId }) |
@Html.ActionLink("Details", "Details", new { productId = item.ProductId }) | //note the productId is filled by item.ProductId
@Html.ActionLink("Delete", "Delete", new { productId = item.ProductId })
</td>
Fail to do so will result all params to be null thus cause the error.
the standard "int" class (int32) does not accept null values, and it will fail a cast from an empty string to int in that case and try and assign null to it.
I would probably take a look at what you're trying to accomplish -- if you're trying to force the administrator to have a productID present for them to edit that record in the database, I would think about putting that into the Request object or some other method that will give some more versatility.
Changing it on the route could implicate other routes that use the same url. To keep it simple and cover all bases.
[HttpGet]
public ActionResult ThePage(int id = -1)
{
if(id == -1)
{
return RedirectToAction("Index");
}
//Or proceed as normal
}
This is also great if this is an Error you are getting when you access the page because you are required to have an id, (for instance... people putting the url in the address bar when they are not supposed to) then set the parameter to an optional value.
EDIT: sorry int, not long as the question asked
精彩评论