Jquery 成功后提交展示欢迎模式
我有表格Jquery 成功后提交展示欢迎模式,jquery,ajax,asp.net-mvc,twitter-bootstrap,form-submit,Jquery,Ajax,Asp.net Mvc,Twitter Bootstrap,Form Submit,我有表格 @使用(Html.BeginForm(“Register”,“Account”,FormMethod.Post,new{id=“registrationForm”})) {…} 我在那里放了一些数据(电子邮件、密码等) 此外,我还有此表单的方法: [HttpPost] [异名] [ValidateAntiForgeryToken] 公共操作结果寄存器(RegisterViewModel模型) {…返回重定向到操作(“索引”);} 在这种方法中,我管理模型(尝试创建新用户)以及是否一切顺
@使用(Html.BeginForm(“Register”,“Account”,FormMethod.Post,new{id=“registrationForm”}))
{…}
我在那里放了一些数据(电子邮件、密码等)
此外,我还有此表单的方法:
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共操作结果寄存器(RegisterViewModel模型)
{…返回重定向到操作(“索引”);}
在这种方法中,我管理模型(尝试创建新用户)以及是否一切顺利。重定向到操作“索引”
问题:我需要在创建新用户之后,但在重定向之前,用一些文本“欢迎…”来显示引导模式。 我怎么做
我尝试为该表单调用
ajax
submit(仅用于显示该模式的成功操作,但没有成功)您可以选择在模型中存储消息,然后在页面上使用razor检查该属性是否有值,如果有,则在页面底部执行脚本
差不多
@if (Model.Message != null){
<script>
//set the message on the dialog
...
//call bootstrap modal
$('#myModal').modal(options);
</script>
}
更新
这是一个我很快就想到的非常粗糙的例子。除此之外,它使用Ajax表单:
使用一个全新的项目,我在Index.cshtml页面上添加了以下内容
@using (Ajax.BeginForm("Register", "Home", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "success" }))
{
@Html.TextBox("name")
<button type="submit" >Register</button>
}
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Welcome, <span id="username"></span>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
@section scripts{
<script>
function success() {
$('#username').text($('#name').val());
$('#myModal').modal();
$('#myModal').on('hidden.bs.modal', function (e) {
window.location = "/Home/About";
});
}
</script>
}
不要忘记包含jquery.unobtrusive-ajax.min.js文件(如果没有安装该软件包,请从nuget获取)在重定向之前可以这样做吗?哦,在重定向之前。。。在这种情况下,您可以使用Ajax.ActionLink提交表单,然后在成功后调用modal。我目前没有确切的语法,但我会在编辑中描述它。谢谢,这很有帮助。我能再问你一次吗?如何在模式窗口中显示服务器错误?如果我使用ajax提交它。谢谢,我会把这个作为一个单独的问题问。但是,我给你举的例子很粗糙。通常,我会将模态创建为局部视图,然后使用AjaxOptions指定插入模式为replace。在控制器中,我将
返回PartialView(“myModalPartial”)代码>在该视图中是显示错误的逻辑
@using (Ajax.BeginForm("Register", "Home", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "success" }))
{
@Html.TextBox("name")
<button type="submit" >Register</button>
}
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Welcome, <span id="username"></span>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
@section scripts{
<script>
function success() {
$('#username').text($('#name').val());
$('#myModal').modal();
$('#myModal').on('hidden.bs.modal', function (e) {
window.location = "/Home/About";
});
}
</script>
}
[HttpPost]
public ActionResult Register(string name)
{
return View("Index");
}