gpt4 book ai didi

ajax - 如何将 Html.BeginForm 变成 Ajax.BeginForm

转载 作者:行者123 更新时间:2023-12-05 00:27:28 25 4
gpt4 key购买 nike

以下有效,但如何将其转换为 Ajax 调用?

<section id="loginWindow">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>

}
</section>

最佳答案

将您的表单更改为:

@using (Ajax.BeginForm("Login", "Account", new AjaxOptions { OnSuccess = "onSuccess" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>
}

如果登录成功,请确保您的登录操作返回重定向 URL,并将此脚本添加到您的 View 中:
<script type="text/javascript">
function onSuccess(result) {
if(result.url) {
window.location.href = result.url;
}
}
</script>

关于ajax - 如何将 Html.BeginForm 变成 Ajax.BeginForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20551571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com