将ID的值从局部视图传递到主视图

时间:2019-06-29 12:25:21

标签: javascript c# asp.net ajax

**控制器代码**

我在局部视图中有以下代码,其ID为loan_no

<html>
<head>


</head>
<body>
    <table class="label-primary" style="width:110%" id="loan_type">
        <tr>
            <td>
                <div class="floatleft" style="height:50px">
                    <div class="label" style="font-size:inherit">Loan Name:</div>

                    <strong>
                        @Html.DropDownList("loan_code",
                                                        (SelectList)ViewBag.loanTypes, "--Select Loan Product--",
                                                        htmlAttributes:new { required = "required",@class = "form-control chosen mandatory",
                                                            @id ="loan_code"})
                        @Html.ValidationMessage("loan_code", "", new { @class = "text-danger" })
                    </strong>
                </div>
            </td>
            <td>
                <div class="label" style="font-size:inherit">*Loan No:</div>
                <span id="loan_no"></span>
            </td>

        </tr>

    </table>
</body>
</html>

主视图代码这是视图中的代码,JavaScript函数savePayslipInfo发出Ajax请求。我需要将部分视图的id'loan_no'中的值传递给该主视图。

<script type="text/javascript">

    function savePayslipInfo() {

        var current_loan_number = $("#loan_no").val();//id is from the partial View posted on the code above
        console.log(current_loan_number)
        $.ajax({
            type: "POST",
            url: "http://localhost:1079/loanapplication/save_Payslip_Info/?loan_no" + current_loan_number,
            data: {
                loan_no: $("#loan_no").val(),
                basic_salary: $("#basic_salary").val(),
                house_allowance: $("#house_allowance").val(),
                other_allowance: $("#other_allowance").val(),
                other_payment: $("#other_payment").val(),
                total_deduction: $("#total_deduction").val(),

            },
            success: function () {
                $('#msg').html("Payslip info saved successfully").fadeIn('slow');
                $('#msg').delay(4000).fadeOut('slow');
            }

        });
    }
</script>

1 个答案:

答案 0 :(得分:0)

由于我试图将html span标签中的InnerText值传递给Ajax中的url, 我使用了javascript'document.getElementById(“ loan_no”)。innerText;' 然后像这样传递它:

$.ajax({
            type: "POST",
            url: "http://localhost:1079/loanapplication/save_Payslip_Info/?loan_no" + document.getElementById("loan_no").innerText,
            data: {
                loan_no: document.getElementById("loan_no").innerText,
                basic_salary: $("#basic_salary").val(),
                house_allowance: $("#house_allowance").val(),
                other_allowance: $("#other_allowance").val(),
                other_payment: $("#other_payment").val(),
                total_deduction: $("#total_deduction").val(),

            },
            success: function () {
                $('#msg').html("Payslip info saved successfully").fadeIn('slow');
                $('#msg').delay(4000).fadeOut('slow');
            }

        });