Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with MVC route when i jQuery AJAX call a route

I'm trying to access a simple ASP.NET MVC route, but it's erroring with:

The parameters dictionary contains a null entry for parameter 'isFoo' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult Transform(Boolean, System.String)' in .. blah blah blah.

Ie. the boolean property is not getting set ... which means the value is not getting passed in.

So i've got an Action method called Transform(..) and it's not accepting the values that are HTTP-Posted to it, with jQuery. It's like my posted values get lost :(

The MVC site is a stock standard ASP.NET MVC File->New->MVC Web Application. Nothing has been changed, with the exception of adding a new controller class. that's it.

using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace WorldDomination.EbonHawk.Web.Controllers
{
    public class AjaxController : Controller
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Transform(bool isFoo, string bar)
        {
            // blah....
            return View();
        }

    }
}

and how this is called, using jQuery ....

$.ajax({
    type: "POST",
    url: "/ajax/transform",
    data: "isfoo=true&bar=" + $("#Bar").val(),
    contentType: "application/json;",
    dataType: "json",
    success: function() { alert("it worked"); },
    failure: function() { alert("Uh oh"); }
});

When i use FireBug, it definately shows that i'm trying to POST to the url. Can anyone help?

like image 507
Pure.Krome Avatar asked Jan 25 '26 22:01

Pure.Krome


2 Answers

At first glance, it appears that you are confusing jQuery...

You are telling it that your data type is JSON, but submitting text. Remove your dataType clause from your AJAX post, and jQuery will figure out and submit your post appropriately.

like image 169
Jeff Fritz Avatar answered Jan 28 '26 15:01

Jeff Fritz


What Jeff said. Alternatively, if you wish to keep the dataType, your data line should look like this:

...
data: { isfoo: true, bar: $("#Bar").val() },
...
like image 27
Darko Z Avatar answered Jan 28 '26 13:01

Darko Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!