Ajax将空值传递给控制器 [英] Ajax passing null value to controller

查看:145
本文介绍了Ajax将空值传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,其中包含ID列表.客户将选择一个,它将在页面上反映总价.我创建了一个ajax调用,当从下拉列表中提取不同的ID时,该调用将更新总数.

I have a dropdown that has a list of ID's in it. The customer will select one and it will reflect a price total on the page. Im creating an ajax call that will update the total when a different ID is pulled from the Dropdown.

$("#BrandId").on('focus', function () {
    // Store the current value on focus and on change
    previous = this.value;
}).change(function () {
    alert("Previous: " +previous);
    sel = this.value;
    alert("Selected: " +sel);
    $.ajax({
        cache: false,
        type: "get",
        contentType: "application/json; charset=utf-8",
        url: '@Url.Action("GetBrandCost", "Shocks")',
        data: JSON.stringify({ idp: previous, id: sel }),
        dataType: "json",
        aysnc: false,
        success: function (data1) {
            alert(data1);
                //ShockTotal = $("#ShockTotal").html();
                //ShockTotal = ShockTotal / 1;
                ////ShockTotal = ShockTotal - data1;
                //$("#ShockTotal").html(data1);

        }
    });
});

警报运行良好,但是ajax并没有将这些ID传递给控制器​​,控制器只是接收到空值.

The alerts are working perfectly but the ajax isnt passing those ID's into the controller, the controller is just receiving nulls.

 public decimal GetBrandCost(string idp, string id)
    {
        decimal costp = 0;
        decimal cost = 0;
        if (id == "" || id == null || idp == "" || idp == null)
        {
            return 0;
        }
        ShockBrand brandp = db.ShockBrands.Find(idp);
        costp = brandp.Cost;
        ShockBrand brand = db.ShockBrands.Find(id);
        cost = brand.Cost;
        cost = cost - costp;
        return cost;
    }

由于它们为null,所以我要敲击if语句,并且在成功内部仅返回零.我读过的大部分内容都是添加内容类型,但对我来说似乎并没有帮助,我敢肯定这有点少.

Since they are null I am hitting my if statement and just returning zero inside the success. Most of the things I read were to add the content type but that didnt seem to help in my case, Im sure it is something little.

推荐答案

在浏览器控制台中,此

$.ajax({
        cache: false,
        type: "get",
        contentType: "application/json; charset=utf-8",
        url: 'http://google.com',
        data: JSON.stringify({ idp: 1, id: 2 }),
        dataType: "json",
        aysnc: false,
        success: function (data1) {
           console.log(data1)

        }
    });

将请求返回到http://google.com/?{%22idp%22:1,%22id%22:2}&_=1440696352799,这是不正确的

returns request to http://google.com/?{%22idp%22:1,%22id%22:2}&_=1440696352799, which is incorrect

并且没有字符串化

$.ajax({
        cache: false,
        type: "get",
        contentType: "application/json; charset=utf-8",
        url: 'http://google.com',
        data: { idp: 1, id: 2 },
        dataType: "json",
        aysnc: false,
        success: function (data1) {
           console.log(data1)

        }
    });

返回http://google.com/?idp=1&id=2&_=1440696381239(请参见网络"标签)

returns http://google.com/?idp=1&id=2&_=1440696381239 (see Network tab)

所以不要使用JSON.stringify

So don't use JSON.stringify

为什么会起作用-您的asp.net控制器动作会接收简单的类型化参数(字符串,数字等),而jquery足够聪明地确定要发送的内容,如果它是对象内部的对象,它将作为对象发送用于POST的POST数据,以及用于GET的对象的字符串表示形式(您具有GET请求,但是出于知识的目的,只需坚持使用两种可以发送的数据类型,即 params & data )因此,当jquery配置url时,asp.net会理解约定并将请求与已采取的操作进行匹配

Why it's gonna work - your asp.net controller action receives simple typed parameters (string, numbers, etc) and jquery is fairly enought smart to determine what are going to send, if it was object inside object it will send it as POST data for POST, and string represenation of object for GET (you have GET request, but for purpose of knowledge, just stick with 2 types of data that can be send, params & data) So when jquery configures url, asp.net understands conventions, and matches request to approciated action

但是不要相信我,自己检查一下

But Don't believe me, check it yourself

chrome开发者控制台是您的朋友

chrome dev console is your friend

这篇关于Ajax将空值传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆