【问题标题】:Pass object from JSONResult to other function in jQuery将对象从 JSONResult 传递给 jQuery 中的其他函数
【发布时间】:2020-11-18 14:45:28
【问题描述】:

如何将我的 Jsonresult 的对象(或我的对象的 id)传递给 jQuery 中的另一个函数?当我的 Actionresults 返回视图或其他操作时,我一切正常,但现在我必须更改所有内容以使其在模式中工作,而且我无法通过第一步。

我的 jQuery:

$(document).ready(function () {
var registerEmailBtn = $("#registerEmailBtn");

registerEmailBtn.click(function (e) {
    e.preventDefault();
    SaveEmailAddress();
    $("#step1").addClass("d-none");
    $("#step2").removeClass("d-none");

});

});


function SaveEmailAddress() {
var user = {
    ID: $("#emailId").val(),
    Email: $("#emailField").val()
};

$.ajax({
    type: "POST",
    contentType: "application/json; charset=UTF-8",
    url: "/api/UserManagement/Register",
    data: JSON.stringify(user),
    dataType: "json",
    success: function (response) {
        console.log(response);
    }
});
}

当我检查控制台时,带有从我的控制器返回的数据的对象就在那里。

控制器功能:

    Function Register(user As User) As JsonResult
        user.ConfirmationCode = codeGenerator.CreateConfirmationCode()
        Dim id = _userData.AddEmailAddressLastId(user)
        user.ID = id
        Return Json(user)
    End Function

输出控制台:

下一步应该是确认代码,当我从 actionresults 返回视图时,这些方法有效,但我似乎无法弄清楚如何将我的对象的 id 传递给下面的方法。

    <HttpGet>
    Function GetConfirmCode(Optional ByVal id As Integer = 0) As JsonResult
        Dim user As New User
        If id > 0 Then
            user = _userData.GetUserByID(id)
            user.ConfirmationCode = ""
        End If
        Return Json(user)
    End Function

    <HttpPost>
    Function ConfirmCode(Optional user As User = Nothing) As JsonResult
        Dim enteredCode = user.ConfirmationCode
        Dim givenCode = _userData.GetConfirmationCode(enteredCode)

        If String.IsNullOrEmpty(givenCode) Or String.IsNullOrWhiteSpace(givenCode) Then
            TempData("ErrorMessage") = "Codes don't match"
            Return Json(user)
        End If

        user = _userData.GetUserWithConfirmationCode(givenCode)
        Return Json(New With {user.ID}, JsonRequestBehavior.AllowGet)

    End Function

我只想将我的注册函数中的 ID 传递给一个确认代码函数,以便我可以从我的数据库中检索正确的记录。我尝试过隐藏字段,但似乎无法填充它们。

HTML:

  @Using Html.BeginForm()

            @<div id="step1">
                @Html.HiddenFor(Function(m) m.ID, New With {.id = "emailId"})
                @Html.TextBoxFor(Function(m) m.Email, New With {.id = "emailField"})
                <button class="mybtn-sm color-btn-default" id="registerEmailBtn">Volgende</button>
                <div class="code-link">
                    <a href="#">Ik heb al een uitnodigingscode ontvangen</a>
                </div>
            </div>
        End Using

        @Using Html.BeginForm()
            @<div id="step2" class="d-none">
                @Html.HiddenFor(Function(m) m.ID, New With {.id = "idField"})
                @Html.TextBoxFor(Function(m) m.Email, New With {.id = "checkEmail"})
                @Html.TextBoxFor(Function(m) m.ConfirmationCode, New With {.id = "confCodeField"})
                <button class="mybtn-sm color-btn-default" id="confCodeBtn">Volgende</button>
            </div>
        End Using
        <div class="login-socials">
            <div class="title">
                <h3>Aanmelden met</h3>
            </div>
            <div class="social-links">
                <div class="social-link-item"><p><i class="fab fa-facebook"></i>&nbsp;Facebook</p></div>
                <div class="social-link-item"><p><i class="fab fa-google"></i>&nbsp; Google</p></div>
                <div class="social-link-item"><p><i class="fab fa-linkedin"></i>&nbsp; linkedin</p></div>
            </div>
        </div>
        @*<a href="#" rel="modal:close">Close</a>*@
    </div>
</div>

【问题讨论】:

  • 您的意思是要将结果传递给另一个控制器方法,比如说确认码?
  • @Adlorem,我想将 JSON 结果中的 ID 作为参数传递给 ConfirmCode 方法。该 Get 方法接受一个可选的 int 作为参数,该参数从具有 ID 的用户的数据库中检索 ConfirmationCode。在我的示例中,ID 71 应该是控制器方法 ConfirmCode 的参数。

标签: jquery ajax asp.net-mvc vb.net


【解决方案1】:

我更改了控制器上的 Register 方法以将 id 返回为 int 而不是对象本身。在ajax调用的成功函数中,我将一个隐藏字段的值设置为传入的int。在下面的 ConfirmCode 方法中,我使用了隐藏字段的值作为参数,并修改了 post 方法,使其也对 Id 进行检查,而不是使用 httpget 和 httppost。

【讨论】:

    猜你喜欢
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多