在Ajax调用之前丢失变量值

时间:2018-03-05 17:09:47

标签: javascript jquery ajax model-view-controller

在下面的代码中,我试图通过AJAX调用将4个值传递给我的控制器。变量“NewType”会丢失其值,而其他三个变量则不会。 NewType从下拉列表中提取其值,而其他三个来自文本框。如果我调试并逐步执行代码,变量将保留其值。如果我运行代码,则清除该值。通过类似的问题,很明显它与异步Ajax调用有关。任何想法都赞赏!!!

HTML

 <tbody>
        <!--Row created for each pesticide listed in Pesticides db table pulled in by Owner-->
        @foreach (PesticideModel p in Model)
        {
            // Only show rows where IsDeleted == 0 (false)
            if (@p.IsDeleted == false)

            {
               <tr class="toggler ui-sortable-handle" onclick="GetPestDetails()">
                    <!--Column for Owner-->
                    <td class="OwnerTD" id="OwnerBox">@p.Owner</td>

                    <!--Column for Brand Name-->
                    <td class="form-group-sm"><input type="text" name="BrandName" id="BrandName" disabled style="width:100%" value="@p.BrandName" /></td>

                   <!--Column for Original Brand Name-->
                   <td class="form-group-sm" style="display:none" id="OrigBrandName">@p.BrandName</td>

                   <!--Column for Pesticides Id-->
                   <td class="form-group-sm" style="display:none" id="PestIdBox">@p.PesticidesID</td>


                    <!--Column for EPA Reg No-->
                    <td class="form-group-sm"><input type="text" name="EPARegNo" id="EPARegNoBox" disabled style="width:100%" value="@p.EPARegNo"  /></td>

                    <!--Column for Type-->
                    <td class="form-group-sm">
                     @*<td class="form-group-sm"><input type="text" name="Type" id="TypeBox" style="width:100%" value="@p.Type" disabled /></td>*@
                     @Html.DropDownListFor(x => @p.Type, (SelectList)ViewBag.Types, @p.Type, new { @class = "form-group-sm", @disabled = "disabled", id = "PType", name = "PType", onchange = "SetHiddenField(); return true;" } )  </td>

                   <!--Column for HiddentType --hidden column-->
                   <td class="HiddenType" id="HiddenType" hidden>@p.Type</td>

                    <!--Column for Editing Buttons-->
                        <td class="btn-group-sm">
                        <button type="button" class="btn btn-primary btn-warning" id="EditPesticideButton" name="EditButton" onclick="EditPesticideAction()">Edit</button>
                        <button type="button" class="btn btn-primary" id="SavePesticideButton" style="display:none" onclick="SavePesticideConfirm()">Save</button>
                        <button type="button" class="btn btn-primary" id="DeleteButton" style="display:none" onclick="DeletePesticideConfirm()">Delete</button>
                        <button type="button" class="btn btn-primary" id="CancelPesticideButton" style="display:none" onclick="CancelPesticideAction()">Cancel</button>
                    </td>

                    <!--Column for IsDeleted --hidden column-->
                    <td class="IsDeleted" id="IsDeleted" hidden>@p.IsDeleted</td>
                </tr>
            } // end if statement

        } @*end foreach statement*@

    </tbody>

JavaScript

 var SavePesticide = function () {

    var NewActiveIngredientName;  //saves new textbox ActiveIngredient field
    var NewEPARegNo;
    var NewType;
    var PesticideID;

    // SaveButton click events
    $("#PesticideTable").on("click", '#SavePesticideButton', function (event) {
        // variables to set Name, Owner, and Id values


        NewType = $(event.currentTarget).closest('tr').find('.PType').text();

        NewActiveIngredientName = $(event.target).closest('tr').find('#ActiveIngredient').val();
        NewEPARegNo = $(event.target).closest('tr').find('#EPARegNoBox').val();
        PesticideID = $(event.currentTarget).closest('tr').find('.PesticideId').text();


        // Make sure values are not empty or null
        if ((NewActiveIngredientName != "" && NewActiveIngredientName != null) && (NewEPARegNo != "" && NewEPARegNo != null)) {
            // send values to UpdatePestices function in controller, then to update database with new values
            $.ajax({
                type: "POST",
                url: '@Url.Action("UpdatePesticides")',
                datatype: 'json',
                contentType: "application/json",
                data: JSON.stringify({PType: NewType, PesticideID: PesticideID, ActiveIngredient: NewActiveIngredientName, EPARegNo: NewEPARegNo}),
                success: function (data) {
                    alert(data);

                    // on success: disable all textbox fields
                    $("#PesticideTable").find("input[type=text]").prop("disabled", true);
                    $(event.target).closest('tr').find("input[type=text]").prop("disabled", true);

                    // on success: show edit button AND hide save, cancel, and delete buttons
                    $(event.target).closest('tr').find('#EditButton').show();
                    $(event.target).closest('tr').find('#SavePesticideButton').hide();
                    $(event.target).closest('tr').find('#CancelBrandButton').hide();
                    $(event.target).closest('tr').find('#DeleteButton').hide();

                    // enable edit buttons
                    $(".btn-warning").prop("disabled", false);

                    // refresh values
                    GetPesticideBrands();
                }
            });
        } else {
            // refresh values
            GetPesticideBrands();
            alert("Active Ingredient and EPA Number must contain a value")
        }
    })

}

控制器

 public string UpdatePesticides(string Owner, string PestType, string OrigBrandName, string BrandName, string EPARegNo)
    {
        try
        {
            ADOHelper helper = new ADOHelper();
            helper.UpdatePesticides(Owner, OrigBrandName, BrandName, EPARegNo, PestType);

            return "Updated Successfully";

        }
        catch (Exception e)
        {
            AgRSys.Classes.Utility.Elmah_Helper.RaiseException(e);
            return null;
        }
    }

0 个答案:

没有答案