KleberBH
KleberBH

Reputation: 462

JQuery not calling controller in asp.net mvc

I am trying to make a call from views to controller using jquery. however it never call controller.

I have a Umbraco 7.1.2 website in asp.net MVC 4 application.

My JS

var dp = jQuery;
dp.noConflict();
dp(document).ready(function() {
    GetTestimonials();
});

function GetTestimonials() {
   dp.ajax({
        type: 'GET',
        url: '/Home/GetTestimonialsList',
        cache: false,
        success: function (data) {
            dp.each(data, function (index, val) {
                alert(val.Name + val.Comment + val.Date);
            });
        },
        error: function () {
            alert("error");
        }
    });
}

Then my controller

[HttpGet]
public JsonResult GetTestimonialsList()
{
     var model = _spdb.TestimonialDetails.Where(t => t.Status == Enums.TestimonialsStatus.approved).Select(t => new { t.Comment, t.Name, t.Date});
     return Json(model, JsonRequestBehavior.AllowGet);
}

I put a breakpoint on the controller, it never got called.

On the browser, no errors were raised with jquery.

However it always triggers alert("error"); when page loads.

I called console.log("test"); it displays on the browser debug.

On same page I am making a form post to controller using jquery and it has no problem at all.

the query from db gives me the data right.

What am I doing wrong?

Thanks a lot

Upvotes: 0

Views: 749

Answers (2)

KleberBH
KleberBH

Reputation: 462

Ok, found the solution. I changed the URL to url: '/umbraco/surface/home/GetTestimonialsList', and works fine.

Upvotes: 1

dotnetstep
dotnetstep

Reputation: 17485

Try this. You are returning IQuerable.

[HttpGet]
public JsonResult GetTestimonialsList()
{
     var model = _spdb.TestimonialDetails.Where(t => t.Status == Enums.TestimonialsStatus.approved).Select(t => new { t.Comment, t.Name, t.Date}).ToList();
     return Json(model, JsonRequestBehavior.AllowGet);
}

Upvotes: 0

Related Questions