I want to create an optional parameter for an ActionResult method.
I have the following:
public ActionResult ViewReq (int id, string Req = null)
When I tried to do the following:
http://localhost/RepMedia/Controller1/ViewReq?id=34343?Req="34233"
I tried the following but got an error:
An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
I am not sure what I am doing wrong.
The problem is with 'id'. The id must be part of the base URI:
http://localhost/RepMedia/Controller1/ViewReq/34343?Req=34233
http://localhost/RepMedia/Controller1/ViewReq?id=34343&Req=34233
Use a question sign before the first parameter - all others should be split by ampersand.
public ActionResult ViewReq (int? id, string Req)
http://localhost/RepMedia/Controller1/ViewReq?id=34343&Req=34233
You don't need make a string parameter optional, as they are reference types whose values will be null anyway if they aren't passed in by MVC. That URL would end up with a non-null "Id", but null "Req".
Related
I'm really struggling with this one. I need a generic list parameter for my Get method, but it needs to be optional. I just did this:
public dynamic Get(List <long> ManufacturerIDs = null)
Unfortunately on runtime i get the error:
Optional parameter 'ManufacturerIDs' is not supported by
'FormatterParameterBinding'.
How to get a generic list as an optional parameter here?
What's the point of using an optional parameter? List<T> is a reference type and if the client doesn't supply a value it will simply be null:
public HttpResponseMessage Get(List<long> manufacturerIDs)
{
...
}
In the index, I have the following link for the object to be edited:
<div class="editor-field">#Html.ActionLink("Edit", "Edit", new { id = thing.Id })</div>
At my controller, I have the following method signature:
public ActionResult Edit(Thing thing)
But This is not called, instead, an error is displayed which specifies null value is passed.
The link contains the required ID value of the object.
Do I need to change the signature of Edit method in the controller ?
Update:
The example fails even with the changes with the error message as
Server Error in '/' Application.
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'MongoDB.Bson.ObjectId' for method 'System.Web.Mvc.ActionResult Edit(MongoDB.Bson.ObjectId)'
Do I need to change the signature of Edit method in the controller ?
Yes, since you are passing only an id parameter in your Html.ActionLink you can't expect to get something more in your Edit action:
public ActionResult Edit(string id)
{
Thing thing = ... go and fetch the thing from the id
...
}
I'm creating a Forum in asp.net MVC3 it contains link of Details which on click will show me details of particular record, but I am getting the following error when I click on the Details link.
The parameters dictionary contains a null entry for parameter 'id' of
non-nullable type 'System.Int32' for method
'System.Web.Mvc.ActionResult Details(Int32)' in
'Prjct_name.Controllers.DefaultController'. An optional parameter must
be a reference type, a nullable type, or be declared as an optional
parameter. Parameter name: parameters
Since I'm very new to MVC dont know how to deal with this
Still you want to use this URL [www.mydomain.com/Default/Details] you can set id as nullbale in controller :
public ActionResult Details(int? id)
{
if (id ==null)
{
// Do stuff
}
else
{ // Do something else
}
}
Sounds like your ActionResult looks like this:
public ActionResult Details(int id)
{
//Do stuff
}
Which would require the url be something like www.mydomain.com/Default/Details/1 where 1 is the id of the item but the url you are hitting is www.mydomain.com/Default/Details without the /[id]. In MVC, if one of your ActionResult prameters is 'id', that parameter is expected to be in the url...not the querystring.
I have and action which takes a userId parameter:
~/Users/Show?userId=1234
Everything works fine except when the userId provided is not an int or is missing.
Then it throws this exception:
Message: The parameters dictionary contains a null entry for parameter 'userId' of
non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Show(Int32,
System.Nullable`1[System.Boolean], System.String)' in 'S4U.Web.Mvc.Controllers.ProfileController'. An optional parameter must be a reference type,
a nullable type, or be declared as an optional parameter.
Parameter name: parameters
..after which the user is redirected to the error page.
How do I configure the route so the action isn't hit at all and it throws a 404 instead?
Don't use a string as suggested somewhere below. Use:
public ActionResult (int userId = 0)
{
}
That's the best practise.
You can also do:
public ActionResult (int? userId)
{
if (userId.HasValue)
{
//...
}
}
As mentioned in my comment on Ufuk Hacıoğulları's answer you should handle validation either through adding a constraint to a route, through having a nullable type if the parameter can be empty.
For the former approach if you have an appropriate constraint this means your route will not be picked up - you will need a catch all route or other error handling. For the nullable type you can test in the action whether it has a value and act accordingly.
Keeping action paramters as strong types is a pattern to aim for.
I'm buiding a UrlHelper for a route
as in best practices
the problem is that the returned value is always null
when debugging in found that
Url.RouteUrl("x") return null
Url.RouteCollection["X"] return Route
i'm trying to do :
public static string Category(this UrlHelper helper, int Id, string category)
{
return helper.RouteUrl("X", new {id = Id, category= category});
}
I can't see where I'm doing something wrong
It appears that this is being caused because you did not specify a default value for {id} and {category} when registering your routes.
Url.RouteUrl("x") will return null because there's no value for id and category provided, and your route definition does not have a default.
I think that you will find if you update your route entry to specify a default value for id and category this will solve your problem. Alternatively, if you are sure to always provide a value for id and category, you can do without it.
As far as your actual Url helper method Category(), that should be working just fine as-is if you are providing a non-null or empty value for id and category. I literally copied the code and it works for me.
For some reason i was still running the mvc release candidate
I installed the mvc 1.0 and now it works fine