简体   繁体   中英

How to pass objects using WebRequest (without model) to External Web Api ASP.NET MVC

I have an asp.net MVC and external WEBAPI. I wanted to pass data in this sequence:

  1. From Ajax to MVC Controller
  2. From MVC Controller to External WEBAPI
  3. External WEBAPI send response
  4. MVC Controller received response
  5. MVC Controller return response to AJAX

Problem: It works saving to database, but all the values is 0. Seems like the objects that I passed is missing somewhere but I don't know where.

Really need help on this. Thanks

Below is my Ajax code calling the MVC Controller:

function savePostApplication(theData) {
 var insert = {};
 insert.JobTitleId = JobTitleId;
 insert.Division = Division;
 insert.SalaryScaleId = SalaryScaleId;
 insert.DepartmentId = DepartmentId;
 insert.MinistryId = MinistryId;

 $.post("/Advert/AddAdvertApplication", theData,
         function (response) {
            alert(response);
 });
}

This is my MVC Controller Codes:

public String AddAdvertApplication(JSON data)
        {
            var url = "https://localhost:44331/api/jobadvertisementdetails";

            var wr = WebRequest.Create(url);
            wr.Method = "POST";

            string json = JsonConvert.SerializeObject(data);
            byte[] byteArray = Encoding.UTF8.GetBytes(json);

            wr.ContentType = "application/x-www-form-urlencoded";

            wr.ContentLength = byteArray.Length;

            var dataStream = wr.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);

            dataStream.Close();

            WebResponse response = wr.GetResponse();

            var Status = ((HttpWebResponse)response).StatusDescription;

            dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);

            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
        }

And this is my WEBAPI Codes:

public String PostJobAdvertisementDetail(JobAdvertisementDetail jobAdvertisementDetail)
        {
            try
            {
                DataTable table = new DataTable();

                String query = @"insert into dbo.JobAdvertisementDetails (JobTitleId, Division, SalaryScaleId, DepartmentId, MinistryId) values (" + jobAdvertisementDetail.JobTitleId +
                    @"," + jobAdvertisementDetail.Division + "," + jobAdvertisementDetail.SalaryScaleId +
                    @","+jobAdvertisementDetail.DepartmentId+","+jobAdvertisementDetail.MinistryId+")";

                using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString))
                using(var cmd = new SqlCommand(query, con))
                using(var da = new SqlDataAdapter(cmd))
                {
                    cmd.CommandType = CommandType.Text;
                    da.Fill(table);
                }
                return "Data Saved";
            }
            catch (Exception ex)
            {
                return "Error";
            }
        }

Update: It works now. I just change the content type from "application/x-www-form-urlencoded" to "application/json"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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