未通过Ajax POST调用MVC Controller中的HttpPost

问题描述 投票:0回答:1

我有一个控制器(InformationTechnologyController)。该控制器包含一个动作(LocationChangeRequest)。该操作采用可选参数(id)。

public ActionResult LocationChangeRequest(ChangeRequestType id = ChangeRequestType.WithinDepartment)

该操作返回带有当前模型数据的视图。

return View(locationChangeRequest);

在该视图内,有一个函数执行ajax发布(下面的代码)以搜索员工信息。

员工搜索1

到达该视图的网址是:

http:// [not relavant here] /InformationTechnology/LocationChangeRequest

[当用户尝试使用route参数访问该视图时,员工搜索功能不起作用。

使用路由参数到达视图的URL为:

http:// [not relavant here] /InformationTechnology/LocationChangeRequest/1

[我发现是在路径中使用/ 1参数时未击中InformationTechnology控制器中的HttpPost方法。尽管它似乎与路径中的参数有关,但我似乎无法弄清楚如何解决该问题。

关于如何处理通过带有参数的url击HttpPost的任何建议,将不胜感激。

HttpPost代码如下:

[HttpPost]
public JsonResult SearchUser(string term)
   {
     ...
     return Json(results, JsonRequestBehavior.AllowGet);
   }

JavaScript代码如下:

$.ajax({
  url: searchUserUrl,
  type: "POST",
  dataType: "json",
  data: { term: request.term },
           success: function (data) {
                    response($.map(data,
                         function (item) { 
                             return { label: item.Name, value: item.HexKey }; }));
                         },
            error: function (xhr, error) {
                    console.debug(xhr); console.debug(error);}
                    })
javascript c# model-view-controller routing
1个回答
0
投票

您需要如下定义使用$的全局变量。

var $ = jQuery.noConflict();

© www.soinside.com 2019 - 2024. All rights reserved.