1

AJAX method :

$(".GetF").click(function (e) {
    e.preventDefault();
    $.post('@Url.Action("test1", "flyer")', { "selectedIds": "1"})
                    .done(function (data) {
                        alert("Data Loaded: " + data);
                    });
});

Controller Action :

    [HttpPost]
    public ActionResult test1(string selectedIds)
    {
       ......
       return Json(list, JsonRequestBehavior.AllowGet);
    }

Everytime i make this ajax call, it calls following method only

[HttpPost]
public ActionResult Index(FViewModel vm)
{
}

I tried changing method to HTTPGet and ajax call to type='get' which also doesn't work.

I'm not able to identify what exactly is the issue here. Please note, there are no submit button or forms on the page. The element for which click event is written is just normal link.

Manish Dhariwal
  • 61
  • 1
  • 1
  • 9

2 Answers2

0

It may be an issue with your routing. Please try posting to the endpoint without using Url.Action() to help rule out routing issues. e.g $.post('http://localhost:53149/test1', { selectedIds: "1"})

Obviously you'll need to replace the port (53149) above with your own but this should help find out if this is where the issue lies. If the problem persist with the direct link then please check your routing table and make sure your test1 method would be hit before being captured by your Index Method.

0

.GetF class is coming from a button ?

I think its a button you are clicking and this buttons type is submit and you are working in your Index page.

change your button type="button" and you dont need to use e.preventDefault();

$.post('@Url.Action("test1", "flyer")',
                {
                    selectedIds: '1',
                }).done(function (data) {
});

I think this will fix your problem.

Mustafa Alan
  • 386
  • 3
  • 17