简体   繁体   中英

HttpPost in MVC Controller not being called through Ajax POST

I have a controller (InformationTechnologyController). That controller contains an action (LocationChangeRequest). That action takes an optional parameter(id).

public ActionResult LocationChangeRequest(ChangeRequestType id = ChangeRequestType.WithinDepartment)

That action returns a view with the current model data.

return View(locationChangeRequest);

Within that view, there's a function that performs an ajax post (code below) to search for employee information.

Employee Search 1

The url to reach that view is:

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

When a user attempts to reach that view using a route parameter, the Employee Search function does not perform.

The url to reach the view with the routing parameter is:

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

What I discovered is the HttpPost method in the InformationTechnology controller is not being hit when using the /1 parameter in the path. Athough it seems to have to do with the parameter in the path, I can't seem to figure out how to solve the problem.

Any advice on how to handle hitting the HttpPost through the url with the parameter would be appreciated.

The HttpPost code is as follows:

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

The javascript code is as follows:

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

You need to define a global variable for using the $ as below.

var $=jQuery.noConflict();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM