简体   繁体   English

ASP.NET MVC jQuery Ajax Post返回null

[英]Asp.net mvc jquery ajax post returns null

I am new to stackoverflow so sorry for the syntax of posting. 我是stackoverflow的新手,所以对发布语法感到抱歉。 my problem is none values are posted or models not getting any value here is my controller function : 我的问题是没有值发布或模型没有任何值,这是我的控制器功能:

[HttpPost]
public JsonResult SendToStoresPost(tempStoreProduct temp)
{
    ViewBag.returenValue = temp.quantity.ToString();
    return Json(ViewBag.returenValue);
}

Here is the model: 这是模型:

public class tempStoreProduct
{
    public int productid;
    public int quantity;
    public int store;        
}

Here is the jquery for ajax post: 这是ajax发布的jquery:

function send(productid)
{
    var quantity = $('#amount-'+productid).val();
    var store = $('#stores').val();
    var temp = {
        productid: productid,
        quantity: quantity,
        store: store
    }
    $.ajax({
        url: "SendToStoresPost", //The url where the server req would we made.
        type: "POST", //The type which you want to use: GET/POST
        data: temp,
        dataType: "JSON", //Return data type (what we expect).
        beforeSend:function(){
            $('#ico-'+productid).html('loading');
        },
        success: function(data) {
            $('#ico-'+productid).html(data);
        }
    });
}

Stuck here for some time. 在这里停留了一段时间。

I think what's wrong is the url in your ajax call. 我认为您的ajax调用中的网址有问题。 I would suggest you to use the @Url.Action and put it in a javascript variable when you render the view like so : 我建议您使用@ Url.Action并在呈现视图时将其放在javascript变量中,如下所示:

<script type="text/javascript">
   var urlSendToStoresPost = '(@Url.Action("SendToStoresPost","ControllerName"))';
</script>

I'm not 100% sure of the syntax above. 我不确定上述语法是否100%。

And later in your ajax call : 然后在您的ajax电话中:

$.ajax({
    url: urlSendToStoresPost, //The url where the server req would we made.
    type: "POST", //The type which you want to use: GET/POST
    data: temp,
    dataType: "JSON", //Return data type (what we expect).
    beforeSend:function(){
        $('#ico-'+productid).html('loading');
    },
    success: function(data) {
        $('#ico-'+productid).html(data);
    }
});
function send(productid)
{
    var quantity = $('#amount-'+productid).val();
    var store = $('#stores').val();
    var temp = {
        productid: productid,
        quantity: quantity,
        store: store
    }
    $.ajax({
        url: "SendToStoresPost", //The url where the server req would we made.
        type: "POST", //The type which you want to use: GET/POST
        data: temp,
        dataType: "JSON", //Return data type (what we expect).
        contentType: "application/json",
        beforeSend:function(){
            $('#ico-'+productid).html('loading');
        },
        success: function(data) {
            $('#ico-'+productid).html(data);
        }
    });
}

Your model properties do not have getters and setters (they are fields!) so the properties cannot be set when posted. 您的模型属性没有getter和setter(它们是字段!),因此发布时无法设置属性。 Change the model to 将模型更改为

public class tempStoreProduct
{
  public int productid { get; set; }
  public int quantity { get; set; }
  public int store { get; set; }       
}

In addition, if you really have 此外,如果您确实有

var quantity = $('#amount-'+productid).val();

meaning you have controls with id="amount-1" , id="amount-2" etc, you need to rethink what you doing and learn to construct html based on your model using strongly typed helpers. 这意味着您具有id="amount-1"id="amount-2"等控件,您需要重新考虑所做的事情,并学习使用强类型帮助器根据模型构造html。

Note also assigning a property to view bag in the POST method and then accessing it again is a bit pointless (as is calling .ToString() )- it can be just 注意还要在POST方法中为视图包分配一个属性,然后再次访问它是没有意义的(就像调用.ToString() )-它可以仅仅是

[HttpPost]
public JsonResult SendToStoresPost(tempStoreProduct temp)
{
  return Json(temp.quantity);
}

although I assume this is not the real code (why pass a value to the server only to return exactly the same value) 尽管我认为这不是真实的代码(为什么将值传递给服务器仅返回完全相同的值)

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

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