简体   繁体   English

数据从 AJAX 调用传递到 Controller 但得到 null 值

[英]Data pass from AJAX call to Controller but getting null values

From below code i want to send data to my controller but when i add debug point to my controller function then receiving all the values null. From below code i want to send data to my controller but when i add debug point to my controller function then receiving all the values null. I dont to replace datatype is text to json.我不替换数据类型是 json 的文本。

jQuery.ajax({
            url: "../VisitorEntry/SaveVisitorEntry",
            type: "POST",
            //data: "{visitpurpose:'" + visitpurpose + "'}",
            data: {
                visitpurpose: visitpurpose,
                txt_visitor_name: txt_visitor_name,
                txt_company_name: txt_company_name,
                txt_emp_name: txt_emp_name,
                txt_phone: txt_phone,
                txt_designation: txt_designation,
                txt_address: txt_address,
                txt_country_name: txt_country_name,
                txt_state_name: txt_state_name,
                txt_city_name: txt_city_name
            },
            dataType: "text",
            traditional: true,
            //contentType: "application/json; charset=utf-8",
            processData: false,
            success: function (response) {

                debugger
                jQuery("#wait").css("display", "none");
                
                QrCodeImage.setAttribute('src', "data:image/jpg;base64," + response);

            },
            error: function (response) {
                debugger
                jQuery("#wait").css("display", "none");
                console.log(response.responseText);
            }
        });

Below is my function on controller where i want to receive all the values but receiving null.下面是我在 controller 上的 function ,我想接收所有值,但接收 null。

[HttpPost]
        public ActionResult SaveVisitorEntry(string visitpurpose, string txt_visitor_name, string txt_company_name, string txt_emp_name, string txt_phone,
            string txt_designation, string txt_address, string txt_country_name, string txt_state_name, string txt_city_name, string txt_emp_number)
        {
            VisitorEntryDA pdm = new VisitorEntryDA();
            DataSet dataSet = new DataSet();
            Bitmap qrCodeImage = null;
            dataSet = pdm.visitorEntry(visitpurpose, txt_visitor_name, txt_company_name, txt_emp_name, txt_phone,
                txt_designation, txt_address, txt_country_name, txt_state_name, txt_city_name, txt_emp_number);
            
            for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
            {
                string iserror = dataSet.Tables[0].Rows[i]["iserror"].ToString();
                string message = dataSet.Tables[0].Rows[i]["message"].ToString();
                string id = dataSet.Tables[0].Rows[i]["visitor_id"].ToString();
                QRCodeGenerator qrGenerator = new QRCodeGenerator();
                //var data = { "visitpurpose":visitpurpose};
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(id, QRCodeGenerator.ECCLevel.Q);
                QRCode qrCode = new QRCode(qrCodeData);
                qrCodeImage = qrCode.GetGraphic(20);
                ImageConverter converter = new ImageConverter();
            }
            
            return Content(Convert.ToBase64String(BitmapToBytes(qrCodeImage)));
        }

For one, make your life easier by changing your parameter list in c# to a single class (in my example DTO just means Data Transfer Object):一方面,通过将 c# 中的参数列表更改为单个 class (在我的示例中,DTO 仅表示数据传输对象),让您的生活更轻松:

public class VisitorDto 
{ 
    public string visitpurpose {get;set;} 
    public string txt_visitor_name {get;set;}  
    public string txt_company_name {get;set;} 
    public string txt_emp_name {get;set;} 
    public string txt_phone {get;set;} 
    public string txt_designation {get;set;} 
    public string txt_address {get;set;}  
    public string txt_country_name {get;set;}  
    public string txt_state_name {get;set;} 
    public string txt_city_name {get;set;} 
    public string txt_emp_number {get;set;} 
 }

And have that in your method like:并在您的方法中使用,例如:

public ActionResult SaveVisitorEntry(VisitorDTO model){
   ...
}

You could also then remove the prefixing of txt_ as it's a bit cumbersom but I'll leave that up to you.然后你也可以删除 txt_ 的前缀,因为它有点麻烦,但我会留给你。

It might just work after that.在那之后它可能会起作用。

Since in your Ajax class your Data: is an object as you've declared it like Data: {} .由于在您的 Ajax class 中,您的Data:是 object ,因为您已将其声明为Data: {}

{} - is object syntax in javascript. {} - 是 javascript 中的 object 语法。 So likely the reason it's null when arriving at your controller is because you've sent an object but the controller is expecting strings.到达您的 controller 时它是 null 的原因很可能是因为您发送了 object 但 Z594C18AB03F2C6E04C 期待字符串。

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

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