I have a Action method like this
[HttpPost]
public ActionResult DoSearchRequestOperation(SearchRequestModelDto data)
{
//..
}
when I us this url string http://localhost:9124/Search/SearchRequest?searchRequestFor=0 , it works. but when i use the url http://localhost:9124/Search/SearchRequest?searchRequestFor= , it is throwing this exception
System.ArgumentException: The parameters dictionary contains a null
entry for parameter ‘searchRequestFor’ of non-nullable type
‘xxxx.Shared.Dto.Process.Search.SearchRequestFor’ for method
‘System.Web.Mvc.ActionResult
SearchRequest(xxxx.Shared.Dto.Process.Search.SearchRequestFor)’ in
‘xxxx.WebServer.UI.Controllers.SearchController
if the user is giving second url it should take the value 0.
I tried changing the maproute like below, but that also is not working.
routes.MapRoute(
"Default1", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Dashboard", id = 0 }
);
Here is the SearchRequestModelDto class
[DataContract]
public class SearchRequestModelDto : UIBoundDto
{
public SearchRequestModelDto()
{
Criteria = new SearchRequestCriteriaDto();
SearchResult = new SearchRequestResultDto();
}
[DataMember]
public SearchRequestCriteriaDto Criteria { get; set; }
[DataMember]
public SearchRequestResultDto SearchResult { get; set; }
}
You could make the
SearchRequestForproperty on yourSearchRequestModelDtoview model a nullable integer if you want to be able to send null values in the query string:The fact that you have set
id=0in your route has no influence on query string parameters. They are not governed by routes so you will have to either write a custom model binder or use a nullable integer on the model. The latter is preferred.