简体   繁体   English

jQuery Ajax和MVC5 httpPost没有收到值

[英]Jquery Ajax and MVC5 httpPost no value received

I try to send values from my view to my controller. 我尝试将值从我的视图发送到控制器。 The method in my controller is called but the value(s) still remain NULL. 我的控制器中的方法被调用,但值仍然为NULL。

Here is the Javascript part: 这是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#) part 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);
    }

What could be the problem with my code ? 我的代码可能是什么问题? why don't I receive any values in my C# part ? 为什么我的C#部分没有收到任何值? It seems that the values get send correctly, at least the payload contains the values I would expect. 似乎值已正确发送,至少有效负载包含了我期望的值。

_data应当具备的使用性能_modelnumberID像以下。

 _data = {'_modelnumberID': '1'}

try below code : 试试下面的代码:

 $.ajax({
        type: 'POST',
        dataType: 'text',
        url: _url,
        contentType: 'application/json',
        data: "_modelnumberID=" + _data,
        success: _successFunctionPointer,
        error: InventValueRequest.HandleError
    });

The Ideal solution would be to use a view model. 理想的解决方案是使用视图模型。

public class Create
{
public string _modelnumberID{get;set;}
}

And your HttpPost action would be accepting an object of this 并且您的HttpPost动作将接受此对象

[HttpPost]
public ActionResult View(Create model)
{
  // do something and return something
}

and ajax will be 和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
    });

Try this. 尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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