在Asp.net MVC项目中通过ajax调用一个动作不起作用

时间:2017-04-16 10:35:25

标签: jquery ajax asp.net-mvc

我正在做一个Asp.net-MVC项目,我想通过ajax调用action IHelp但它根本不工作,在调试器中,代码没有成功循环,似乎有些问题与控制器

这是我的控制器:

 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);

    }

这是我的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>

这是我的ajax代码

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


        success: function (result) {

            alert();
        }
    });
});

这就是我在路线配置中的内容

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 }
        );
    }

1 个答案:

答案 0 :(得分:1)

由于您的默认路由为{id},因此会将/1视为参数id,但在您的操作中,参数名称为int dataid,这就是为什么它会获得0 dataid 1 {} 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); } 所以你可以做到

dataid

将名称从id更改为dataid,或者您可以像这样设置url: '/Home/IHelp?dataid=' + $(self).data('id'),

<any>