ASP.NET MVC and JQuery get info to controller

前端 未结 7 1415
轻奢々
轻奢々 2020-12-28 23:51

I am totally confused on how to do ajax stuffs with jQuery and it seems the more I try the more confused I get. At this point all I want to do is get data to my controller

7条回答
  •  滥情空心
    2020-12-29 00:04

    It has to do with the way you're structuring your request. Your JQuery call is sending the data to the AddLink action on the User controller as POST data, which means in your C# code, you'll access it via the Request.Form object. With what you're trying to do, you'd structure the jQuery URL as

    /User/AddLink/{Title}/{URL}
    

    This would require you to write a rule in your Global.ASAX file that handled that sort of input. In short, if you just modify your AddLink method as follows:

    public ActionResult AddLink()
    {
        return Json(new { Result = string.Format(Request.Form["title"] + " " + Request.Form["url"])});
    }
    

    I believe you'll get the response you're looking for.

提交回复
热议问题