Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am stuck in one simple issue. I need a redirection from existing URL to another URL taking some parameters from current DOM. Basically, I need to call another action method of a controller along with passing some parameters.

What I have tried:

Following in my anchor element:

<a id="Link1" onclick="callActionMethod(this.id);">Link1</a>

Following is the Javascript Code:

function callActionMethod(linkId) {
//Get DOM element values - Param 1, Param2, Param3, Param4
window.location.href = Url.Action("MyActionMethod", "MyController") + '?Param1=' + param1Value + '&Param2=' + param2Value + '&Param3=' + param3VAlue + '&Param4=' + param4VAlue;
}



Action Method:
C#
public ActionResult MyActionMethod (DateTime Param1, DateTime Param2, string Param3, string Param4)
{
	//Do Some Work
	//Return View
}


However, I am getting the following error while clicking the link1:
C#
The parameters dictionary contains a null entry for parameter 'Param1' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult MyActionMethod(System.DateTime, System.DateTime, System.String, System.String, System.String, System.String, Boolean, Boolean)' in '~.Controllers.Common.MyController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters



I guess it is some routing error. Following is the route.config entry:
C#
routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = defaultController, action = defaultAction, id = UrlParameter.Optional }
            );


I dont think I can change the route.config file and make one more entry to accommodate these many parameters. How else can I make this work? Any other approach for redirecting? Any help will be appreciated.
Posted
Updated 4-Apr-16 20:56pm
Comments
Krunal Rohit 5-Apr-16 2:50am    
Try your parameters this way:

public ActionResult MyActionMethod (DateTime? Param1, DateTime? Param2, string Param3, string Param4)
{
//Do Some Work
//Return View
}

KR
NJ44 6-Apr-16 4:41am    
yes this works.. thanks Krunal

1 solution

Either you need to make the param1 data type as Nullable DataTime
C#
DateTime? (or) Nullable<datetime> </datetime>


while passing the date value from the javascript, cast it to a proper date time.using
JavaScript
javascriptDateObject.toUTCString()

or using Stringify, check the below url
jquery - Pass a datetime from javascript to c# (Controller) [^]
 
Share this answer
 
Comments
NJ44 6-Apr-16 4:41am    
Thanks again.. Karthik :) making he parameters Nullable with few more changes in the function code worked.
Karthik_Mahalingam 6-Apr-16 4:52am    
welcome NJ44 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900