Jquery Ajax和MVC5 httpPost未收到任何值
本文关键字:任何值 httpPost Ajax MVC5 Jquery | 更新日期: 2023-09-27 18:28:31
我尝试将视图中的值发送到控制器。调用了我的控制器中的方法,但值仍然为NULL。
这是Javascript部分:
GUIRequests.prototype.SetNewItemDeliveryValues = function () {
var modelNumberID = this.GetValue('#ModelNumberID');
this.Request.GetPreOrderIDForModelNumberID(modelNumberID); //InventValueRequest
this.Request.GetShortfallAndOverdeliveryInNewItemDelivery(modelNumberID);
}
InventValueRequest.prototype.GetPreOrderIDForModelNumberID = function (_modelNumberID) {
this.CallAjax("/NewItemDelivery/GetPreOrderIDForModelNumberID", _modelNumberID, CallbackMethods.SetPreOrderID);
}
//Private
InventValueRequest.prototype.CallAjax = function (_url, _data, _successFunctionPointer) {
$.ajax({
type: 'POST',
url: _url,
contentType: 'application/json',
data: JSON.stringify(_data),
success: _successFunctionPointer,
error: InventValueRequest.HandleError
});
}
Asp.Net MVC5(C#)部件
[HttpPost]
public ActionResult GetPreOrderIDForModelNumberID(string _modelnumberID)
{
String preOrderID = "";
if (_modelnumberID == null)
{
preOrderID = "No value received";
}
else
{
//Do something
}
return Json(preOrderID);
}
我的代码可能有什么问题?为什么我的C#部分没有任何值?看起来这些值被正确地发送了,至少有效负载包含了我期望的值。
_data
应该具有如下属性_modelnumberID
。
_data = {'_modelnumberID': '1'}
尝试以下代码:
$.ajax({
type: 'POST',
dataType: 'text',
url: _url,
contentType: 'application/json',
data: "_modelnumberID=" + _data,
success: _successFunctionPointer,
error: InventValueRequest.HandleError
});
理想的解决方案是使用视图模型。
public class Create
{
public string _modelnumberID{get;set;}
}
您的HttpPost操作将接受这个的对象
[HttpPost]
public ActionResult View(Create model)
{
// do something and return something
}
ajax将成为
$('.submit').click(function () {
var myData = {
_modelnumberID: _data
}
$.ajax({
url: '/Controller/Action',
type: 'POST',
data: myData,
processData: false
}).done(function () {
}).fail(function () {
});
});
$.ajax({
type: 'POST',
url: _url+"?_modelnumberID="+ _data,
success: _successFunctionPointer,
error: InventValueRequest.HandleError
});
试试这个。