【问题标题】:Passing value of ID from partial view To the main View将 ID 的值从部分视图传递到主视图
【发布时间】:2019-06-29 12:25:21
【问题描述】:

** 控制器代码**

我的部分视图中有以下代码,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>

【问题讨论】:

  • 如果你有id为loan_no的输入控件,你可以在主视图的JavaScript代码中使用它。
  • @ChetanRanpariya 我没有,我想把它用作url中的变量。
  • 你正在做$("#loan_no").val(),,因为你需要拥有ID为loan_no的输入控制。您在部分视图中有loan_code 下拉列表,如果它具有您想要的相同值,那么您也可以使用$("#loan_code").val(),
  • @ChetanRanpariya 这样我可以进行隐藏输入吗?喜欢
  • 是的..你也可以做隐藏输入...

标签: javascript c# asp.net ajax


【解决方案1】:

因为我试图将 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');
            }

        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多