来自url的MVC JQuery调用,无操作 [英] MVC JQuery call from url without action

查看:59
本文介绍了来自url的MVC JQuery调用,无操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有localhost:51775/Book/Index/bookname页面,它正常工作.但是我想要从URL中删除Index并使它显示为localhost:51775/Book/bookname,我可以根据需要进行页面链接,但是这次jquery调用不起作用.

I have localhost:51775/Book/Index/bookname page and it working without problem. But what I want is to remove Index from url and make it appear as localhost:51775/Book/bookname , I can do the page link as I wanted but this time jquery calls do not work.

我已将新路由添加为routep

I have added a new route to routeconfig as

routes.MapRoute(
            "Book",
            "Book/{id}",
            new { controller = "Book", action = "Index", id = RouteParameter.Optional }
        );

通过此URL进行的jQuery调用为 http://localhost:51775/Book/bookname 没有工作;

Jquery call from this url as http://localhost:51775/Book/bookname doesn't work;

 var data = new FormData();
    data.append("comment", comment);
    data.append("id", id);

var ajaxRequest = $.ajax({
        type: "POST",
        url: "/Book/AddBookComment",
        contentType: false,
        processData: false,
        data: data
    });

但是如果我删除routeconfig,则它将作为 http://localhost:51775/Book/Index/bookname

but if I remove routeconfig then it works as http://localhost:51775/Book/Index/bookname

这是从jquery调用的c#方法;

Here is the c# method which is called from jquery;

[HttpPost]
    public string AddBookComment()
    {
        string comment = Request.Form["comment"];
        string id = Request.Form["id"];

        Int64 idBook = id.ToInt64();
        SuggestBusiness.Instance.AddBookComment(idBook, SessionManager.GetSession().CurrentMember.MemberId, comment);
        return "ok";
    }

localhost:51775/Book/Index/bookname是工作页面链接,localhost:51775/Book/bookname是我希望它工作的页面链接,localhost:51775/Book/AddBookComment是jquery的c#方法

localhost:51775/Book/Index/bookname is the working page link, localhost:51775/Book/bookname is the page link that I want it to work, localhost:51775/Book/AddBookComment is the c# method for jquery

我该怎么做才能进行该jquery调用?

What should I do to make that jquery call?

推荐答案

由于您的路由带有 url:"Book/{id}" ,这意味着在您的ajax调用中,url:"/Book/AddBookComment",会匹配该路由,并将您发送到 Index()方法.要么删除路线,要么可以添加其他特定路线(需要在您的"Book" 路线之前)

Since you have a route with url: "Book/{id}" it means that in you ajax call, url: "/Book/AddBookComment", will match that route and send you to the Index() method. Either remove the route or you can add another specific route (needs to be before your "Book" route)

routes.MapRoute(
  "BookComment",
   "Book/AddBookComment",
   new { controller = "Book", action = "AddBookComment" }
);
.... // your other routes

这篇关于来自url的MVC JQuery调用,无操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆