Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

c# - Why doesn't the action method accept a parameters from the ajax call?

I am sending the FileHeadNo from ajax to my controller action but it doesn't receive the FileHeadNo.

 $(function () {
        $("#txtReportNo").autocomplete({
            source: function (request, response) {

                var reportNo = $("#txtReportNo").val();
                var FileHeadNo = $("#txtFileHeadNo").val();
                var InitialsID = ""; //$("#CNGInspectionReport_InitialsID").val();
                var ProjectID = ""; //$("#CNGInspectionReport_ProjectID").val();
                var url;
                var data;
                var onlyReport = 0;
                // console.log(InitialsID + " " + ProjectID);

                //if (($("#CNGInspectionReport_InitialsID").val() == null || $("#CNGInspectionReport_InitialsID").val() == "" || $("#CNGInspectionReport_InitialsID").val() == "0") && ($("#CNGInspectionReport_ProjectID").val() == "" || $("#CNGInspectionReport_ProjectID").val() == "0")) {

                url = "/CNGInspectionReport/GetInspectionReportFilteredIssuedOnly"
                reportNo = { reportNo: $("#txtReportNo").val() };
                data = JSON.stringify(reportNo);
                onlyReport = 1;

                if (onlyReport == 1) {
                    data = JSON.stringify(reportNo);
                }
                else {
                    var Listdata = { reportNo: $("#txtReportNo").val(), FileHeadNo: $("#txtFileHeadNo").val() };       
                    data = JSON.stringify(Listdata); 
                }

                $.ajax({
                    url: url,
                    data: data,
                    dataType: "json",
                    type: "POST",
                    cache: false,
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        //debugger;
                        //response(JSON.parse(data));
                        response($.map(data.result, function (item) {
                            return {
                                value: item   //item.VelosiReportNo
                            }
                        }))
                    },
                    //error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //    var err = eval("(" + XMLHttpRequest.responseText + ")");
                    //    alert(err.Message)
                    //}
                });
            },
            minLength: 1 //This is the Char length of inputTextBox
        });
    });

This is the action method but it is receiving the FileHeadNo as empty.

public ActionResult GetInspectionReportFilteredIssuedOnly(string reportNo, string FileHeadNo= "")
        {
            try
            {

I tried everything but it goes empty. Please help me on this thing.

question from:https://stackoverflow.com/questions/65883262/why-doesnt-the-action-method-accept-a-parameters-from-the-ajax-call

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This code:

               url = "/CNGInspectionReport/GetInspectionReportFilteredIssuedOnly"
                reportNo = { reportNo: $("#txtReportNo").val() };
                data = JSON.stringify(reportNo);
                onlyReport = 1;

                if (onlyReport == 1) {
                    data = JSON.stringify(reportNo);
                }
                else {
                    var Listdata = { reportNo: $("#txtReportNo").val(), FileHeadNo: $("#txtFileHeadNo").val() };       
                    data = JSON.stringify(Listdata); 
                }

replace with:


url = "/CNGInspectionReport/GetInspectionReportFilteredIssuedOnly"

var data;

 if (onlyReport == 1) {
   data = {reportNo:reportNo, FileHeadNo:""};
  }
  else {
  data = {reportNo:reportNo, FileHeadNo:FileHeadNo};
 }

Remove this code from ajax:

dataType: "json",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },

and add [FromBody] to your action header:

public ActionResult GetInspectionReportFilteredIssuedOnly([FromBody] string reportNo, string FileHeadNo= "")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...