Контроллер Web API POST & PUT & DELETE работает неправильно - PullRequest
0 голосов
/ 19 ноября 2018

Я получаю результаты как ложные при запуске моих методов POST & PUT & DELETE.Когда я использовал POSTMAN, я получаю 200 OK, но мой ответ возвращается как ложный?

Вот что я получаю, когда использую POST: https://i.stack.imgur.com/fytB1.png

Вот что я получаю, когда использую UPDATE: https://i.stack.imgur.com/y3vdv.png

Вот что я получаю, когда использую DELETE: https://i.stack.imgur.com/hSNjy.png

Вот мой код контроллера:

[HttpPost("AddNewOwner")]
    public Boolean AddNewOwner(Owner theOwner)
    {
        DBConnect objDB = new DBConnect();
        string strSQL = "INSERT INTO HomeOwnership_T (HomeOwnerID, FirstName, LastName, Address, City, State, ZipCode, TelNo, Email, BlockNo, LotNo, SaleDate, SalePrice, IsSold) " +
            "VALUES ('" + theOwner.HomeOwnerID + "', '" + theOwner.FirstName + "', '" +
            theOwner.LastName + "', '" + theOwner.Address + "', '" + theOwner.City +
            "', '" + theOwner.State + "', '" + theOwner.ZipCode + "', '" + theOwner.TelNo + "', '"
            + theOwner.Email + "', '" + theOwner.BlockNo + "', '" + theOwner.LotNo + "', '"
            + theOwner.SaleDate + "', '" + theOwner.SalePrice + "', '" + theOwner.IsSold + "')";

        //Execute the INSERT statement in the database
        int result = objDB.DoUpdate(strSQL);

        if (result > 0)
        {
            string command = "SELECT TOP 1 HomeOwnerID FROM HomeOwnership_T ORDER BY DESC";
            DataSet ds = objDB.GetDataSet(command);
            Tax taxInfo;
            foreach (DataRow record in ds.Tables[0].Rows)
            {
                taxInfo = new Tax();
                taxInfo.HomeOwnerID = int.Parse(record["HomeOwnerID"].ToString());
                string strSQL2 = "INSERT INTO TaxInfo_T (AccessedVal, LandVal, AdditionalVal, TaxRate, TaxPerYear, RealEstateTax, HomeOwnerID)"
                    + "VALUES (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '" + taxInfo.HomeOwnerID + "')";
                objDB.DoUpdate(strSQL2);
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    [HttpPut]
    public Boolean Update(Owner theOwner)
    {
        DBConnect objDB = new DBConnect();
        HomeTax owner = new HomeTax();
        string strSQL = "UPDATE HomeOwnership_T SET HomeOwnerID = " + theOwner.HomeOwnerID +
            ", FirstName = '" + theOwner.FirstName + "', LastName: '" + theOwner.LastName +
            "', Address = '" + theOwner.Address + "', City = '" + theOwner.City +
            "', City = '" + theOwner.City + "', State = '" + theOwner.State +
            "', ZipCode = '" + theOwner.ZipCode + "', TelNo = '" + theOwner.TelNo +
            "', Email = '" + theOwner.Email + "', BlockNo = " + theOwner.BlockNo +
            ", LotNo = " + theOwner.LotNo + "', SaleDate = '" + theOwner.SaleDate +
            "', SalePrice = " + theOwner.SalePrice + ", IsSold = '" + theOwner.IsSold +
            " WHERE HomeOwnerID = " + theOwner.HomeOwnerID;

        int result = objDB.DoUpdate(strSQL);

        if (result > 0)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    [HttpDelete("{id}")]
    public Boolean Delete(int id)
    {
        DBConnect objDB = new DBConnect();
        string strSQL = "DELETE * FROM HomeOwnership_T INNER JOIN TaxInfo_T " +
            "ON HomeOwnership_T.HomeOwnerID = TaxInfo_T.HomeOwnerID WHERE HomeOwnerID = " + id;
        int result = objDB.DoUpdate(strSQL);

        if(result > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Вот мои вызовы ajax:

$(document).on("click", "#btnAddHomeOwner", function () {

            var strURL = "https://localhost:44395/api/ServiceDeed/AddNewOwner";

            $("#msg").html("");
            $("#update").html("");
            $("#updateResult").html("");

            console.log("btnAddHomeOwner selected");

            var owner = new Object();
            owner.HomeOwnerID = $("#txtHomeOwnerID").val();
            owner.FirstName = $("#txtFirstName").val();
            owner.LastName = $("#txtLastName").val();
            owner.Address = $("#txtAddress").val();
            owner.City = $("#txtCity").val();
            owner.State = $("#txtState").val();
            owner.ZipCode = $("#txtZipCode").val();
            owner.TelNo = $("#txtTelNo").val();
            owner.Email = $("#txtEmail").val();
            owner.BlockNo = $("#txtBlockNo").val();
            owner.LotNo = $("#txtLotNo").val();
            owner.SaleDate = $("#txtSaleDate").val();
            owner.SalePrice = $("#txtSalePrice").val();
            owner.IsSold = $("#txtIsSold").val();

            var strInput = JSON.stringify(owner);

            // Make an AJAX request to get a home and store the response in the appropriate div.
            $.ajax({
                type: "POST",
                url: strURL,
                contentType: "application/json", // set the data type sent to the Web Service.
                dataType: "json",                               // set the data type expected from the Web Service.
                data: strInput,                                 // send an empty JSON object (no input required).
                success: function (data) {                      // set callback function used to update the page/
                    console.log(data);

                    var result = data;

                    if (result == true)
                        $("#msg").text("The record was successfully added to the database.");
                    else
                        $("#msg").text("The record was not added to the database. Try again later.");
                },
                error: function (req, status, error) {          // sets the error callback function used when an error occurs.
                    alert("Error: " + req.responseText + " | " + status + " | " + error);
                }

            }); //end of ajax method
        }); // end of btnStoreHomeOwner click event

        $(document).on("click", "#btnDelete", function () {

            var strURL = "https://localhost:44395/api/ServiceDeed/";

            var param = $("#txtHomeOwnerID").val();

            $("#msg").html("");
            $("#update").html("");
            $("#updateResult").html("");

            console.log("Delete button selected.");

            $.ajax({
                type: "DELETE",
                url: strURL + param,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{5}",
                success: function (data) {
                    console.log(data);

                    var result = data;

                    if (result == true) {
                        $("#msg").text("The record with HomeOwnerID: " + param + " was deleted.");
                        alert("Deleted!");
                    }
                    else {
                        $("#msg").text("The record with HomeOwnerID: " + param + " was not deleted.");
                        alert("Not Deleted");
                    }
                },
                error: function (req, status, error) {          // sets the error callback function used when an error occurs.
                    alert("Error: " + req.responseText + " | " + status + " | " + error);
                }
            }); //end of ajax method
        }); // end of btnDelete click event


$(document).on("click", "#btnUpdateAll", function () {

            var strURL = "https://localhost:44395/api/ServiceDeed/Update";

            // Clear the divs contents.
            //$("#display").html("");
            //$("#msg").html("");
            $("#update").html("");
            $("#updateResult").html("");

            console.log("Update home owner button selected.");

            var owner = new Object();
            owner.HomeOwnerID = $("#txtHomeOwnerID").val();
            owner.FirstName = $("#txtFirstName").val();
            owner.LastName = $("#txtLastName").val();
            owner.Address = $("#txtAddress").val();
            owner.City = $("#txtCity").val();
            owner.State = $("#txtState").val();
            owner.ZipCode = $("#txtZipCode").val();
            owner.TelNo = $("#txtTelNo").val();
            owner.Email = $("#txtEmail").val();
            owner.BlockNo = $("#txtBlockNo").val();
            owner.LotNo = $("#txtLotNo").val();
            owner.SaleDate = $("#txtSaleDate").val();
            owner.SalePrice = $("#txtSalePrice").val();
            owner.IsSold = $("#txtIsSold").val();

            var strInput = JSON.stringify(owner);

            //Make an AJAX request to get a home owner and display the response in the appropriate div
            $.ajax({
                type: "PUT",
                url: strURL,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: strInput,
                success: function (data) {
                    console.log(data);

                    var owner = data;
                    $("#display").html("<hr><p>".concat("HomeOwnerID: ", owner.HomeOwnerID,
                        "<br>FirstName: ", owner.FirstName, "<br>LastName: ", owner.LastName,
                        "<br>Address: ", owner.Address, "<br>City: ", owner.City,
                        "<br>State: ", owner.State, "<br>ZipCode: ", owner.ZipCode,
                        "<br>Telephone Number: ", owner.TelNo, "<br>Email: ", owner.Email,
                        "<br>Block Number: ", owner.BlockNo, "<br>Lot Number: ", owner.LotNo,
                        "<br>Date of Sale: ", owner.SaleDate, "<br>Sale Price: $", owner.SalePrice,
                        "<br>Sold Status: ", owner.IsSold, "<br>Accessed Value: $", owner.AccessedVal, 
                        "<br>Land Value: $", owner.LandVal, "<br>Additional Value: $", owner.AdditionalVal,
                        "<br>Tax Rate: $", owner.TaxRate, "<br>Tax Per Year: $", owner.TaxPerYear,
                        "<br>Real Estate Tax: $", owner.RealEstateTax, "</p >"));

                    if (owner == true)
                        $("#updateResult").text("The record was successfully updated to the database.");
                    else
                        $("#updateResult").text("The record was not updated to the database. Try again later.");
                },
                error: function (req, status, error) {          // sets the error callback function used when an error occurs.
                    alert("Error: " + req.responseText + " | " + status + " | " + error);
                }
            }); //end of ajax method
        }); //end of btnUpdate 

1 Ответ

0 голосов
/ 20 ноября 2018

проверьте строку sql, когда вы устанавливаете homeownerid. Предполагается, что это "SET hoID = '" + theOwner.HomeOwnerID + "', ownerName = '" + theOwner.ownerName + "' .... etc

...