Hulk
Hulk

Reputation: 107

Error in constructed URL for getJSON method

I have the following jQuery call in the razor view.

$.getJSON('/Controller/ActionName/'+ @ViewContext.RouteData.Values["Name"] + '?Date=' + ('#StartDate').val(), function (data) {
    alert(data);
    });

The error in chrome browser console is that the below URL has returned no page found.

http://localhost:{portNumber}/Controller/ActionName/John?Date=9/21/2014&_=1413867422739

it is true because of the added extra token to the end of the url.
Can any one please let me know the reasons for the extra token?
I have an appropriate method in the controller and not able to figure out a solution.
The routeConfig.cs file is not changed and it has the default values to it.
Please let me know in comments if you need further information. I am not able to understand which information to furnish.

Route information:

{
    routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", name = UrlParameter.Optional }
        );
}

Action Signature in the controller

public JsonResult ActionName(string**?** name, DateTime startDate)
        {
            var model = new ViewModel();
            model.loadItems(name**.value**, startDate);
            return Json(model.Data,  **JsonRequestBehavior.AllowGet**);
        }

Answer:
Made the above changes in surrounded by ** and the code worked.
Thank you for the comments and answers.
Update:
I have mentioned the default values in order to hide the unrequired information.
localhost is correct and other pages are working fine just a small part of ui which is related to this ajax call is not working.
No controller is name something else.

Can you please provide a solution for sending date values in URLS?

Upvotes: 0

Views: 199

Answers (2)

user1364100
user1364100

Reputation:

 var StartDate= ('#StartDate').val();    
 var url="/Controller/ActionName/'+ @ViewContext.RouteData.Values["Name"] + '/";

     $.ajax({
     url: url,
     data: { Date: StartDate},
     cache: false,
     type: "POST",
    success: function (data){
         },
    error: function (data){
         }
    });

Upvotes: 0

user3559349
user3559349

Reputation:

Change you call to (assuming your controller is really named ControllerController

$.getJSON('/Controller/ActionName/', { name: @ViewContext.RouteData.Values["Name"], startDate: $('#StartDate').val(), function (data) {..

You have a few problems here. Your default route accepts a parameter named ID but your method only has parameters name and startDate. You are not passing any parameters with those names (you are passing one for date but that does not match the method signature parameter)

Note you should not hard code the controller and action names this way. The recommend ways is

var url = '@Url.Action("ActionName", "Controller")';

Upvotes: 1

Related Questions