neda Derakhshesh
neda Derakhshesh

Reputation: 1163

in Asp.net MVC Project calling an action through ajax not working

Im doing an Asp.net-MVC Project and I want to call action IHelp through ajax but its not working at all, in debugger, code doesn't come into success loop, It seems some problem with controller

here is my controller:

 public JsonResult IHelp(int dataid)
    {
        var ajaxq = db.Questions.Where(e => e.CategoryId == dataid).Select(e => new
        {
            quest = e.Qu,
            answe = e.Ans
        }).ToList();

        return Json(ajaxq);

    }

this is my html

<nav class="row" id="iconsinhelp">
    <ul class="">
        <li data-id=1 class="col-md-2">

            <div class="">
                <div class="margin0auto  width80px">
                    <img src="~/Content/img/help/before-you-travel-0.png" />
                    <img src="~/Content/img/help/before-you-travel-1.png" />
                </div>

            </div>
            <p class="">Before You Travel</p>

        </li>
        <li data-id=2 class="col-md-2">

            <div>
                <div class="margin0auto  width80px">
                    <img src="~/Content/img/help/your-flights-0.png" />
                    <img src="~/Content/img/help/your-flights-1.png" />
                </div>

            </div>
            <p>You'r Flights</p>


        </li>
        <li data-id=3 class="col-md-2">

            <div>

                <div class="margin0auto  width80px">
                    <img src="~/Content/img/help/on holiday-0.png" />
                    <img src="~/Content/img/help/on holiday-1.png" />
                </div>


            </div>
            <p>On Holiday</p>

        </li>
        <li data-id=4 class="col-md-2">

            <div>

                <div class="margin0auto  width80px">
                    <img src="~/Content/img/help/when-you-hetback-0.png" />
                    <img src="~/Content/img/help/when-you-hetback-1.png" />

                </div>


            </div>
            <p>When You Get Back</p>

        </li>
        <li data-id=5 class="col-md-2">

            <div>

                <div class="margin0auto  width80px">

                    <img src="~/Content/img/help/travel-advice-&-safety-0.png" />
                    <img src="~/Content/img/help/travel-advice-&-safety-1.png" />

                </div>


            </div>
            <p>Travel Advice & Safety</p>

        </li>
        <li data-id=6 class="col-md-2 ficon">

            <div>
                <div class="margin0auto  width80px">
                    <img src="~/Content/img/help/payment-0.png" />

                    <img src="~/Content/img/help/payment-1.png" />
                </div>





            </div>
            <p>Payments</p>

        </li>
    </ul>
</nav>

and this is my ajax code

   $("#iconsinhelp li").click(function () {
    var self = this;
    alert($(self).data('id'));
    $.ajax({
        url: '/Home/IHelp/' + $(self).data('id'),
        type: 'POST',


        success: function (result) {

            alert();
        }
    });
});

and this is what I put in route config

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Upvotes: 0

Views: 35

Answers (1)

Usman
Usman

Reputation: 4703

since your default route has {id} it will consider the /1 as parameter id but in your action the name of parameter is int dataid thats why it will get 0 in the dataid so you can do

 public JsonResult IHelp(int id)
    {
        var ajaxq = db.Questions.Where(e => e.CategoryId == id).Select(e => new
        {
            quest = e.Qu,
            answe = e.Ans
        }).ToList();

        return Json(ajaxq);

    }

change name from dataid to id or you can set dataid like this

url: '/Home/IHelp?dataid=' + $(self).data('id'),

Upvotes: 1

Related Questions