user2915962
user2915962

Reputation: 2711

Render partial view with AJAX-call to MVC-action

I have this AJAX in my code:

 $(".dogname").click(function () {
        var id = $(this).attr("data-id");
        alert(id);
            $.ajax({
                url: '/Home/GetSingleDog',
                dataType: 'html',
                data: {
                    dogid: id,

                },
                success: function (data) {
                    $('#hidden').html(data);

                }
            });

    });

The alert gets triggered with the correct value but the AJAX-call does not start(the method does not get called).

Here is the method that im trying to hit:

 public ActionResult GetSingleDog(int dogid)
        {
            var model = _ef.SingleDog(dogid);

            if (Request.IsAjaxRequest())
            {
                return PartialView("_dogpartial", model);
            }
            else
            {
                return null;
            }
        }

Can someone see what i am missing? Thanks!

Upvotes: 0

Views: 1224

Answers (1)

Mox Shah
Mox Shah

Reputation: 3015

do you know what error does this ajax call throws? Use fiddler or some other tool to verify response from the server.

try modifying your ajax call as following

$.ajax({
                url: '/Home/GetSingleDog',
                dataType: 'string',
                data: {
                    dogid: id,

                },
                success: function (data) {
                    $('#hidden').html(data);

                }
                error: function(x,h,r)
                {
                    //Verify error 
                }
            });

Also try

$.get("Home/GetSingleDog",{dogid : id},function(data){
$('#hidden').html(data);
});

Make sure, URL is correct and parameter dogid(case sensitive) is same as in controller's action method

Upvotes: 1

Related Questions