1.新建Controller

public ActionResult Index()
{
return View();
} public ActionResult Person(int? id)
{
//could add code here to get model based on id....
return PartialView("_Person");
//calling partial with existing model....
//return PartialView("_Person", model); } [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Person(PersonValidationViewModel model)
{
if (!ModelState.IsValid)
{
var errors = GetErrorsFromModelState();
return Json(new {success = false, errors = errors});
//return PartialView("_Person", model);
} //save to persistent store;
//return data back to post OR do a normal MVC Redirect....
return Json(new {success = true}); //perhaps you want to do more on return.... otherwise this if block is not necessary....
//return RedirectToAction("Index");
}

Controller相关代码

2.新建相应的index
<div class="jumbotron">
<h1>@ViewBag.Title</h1>
<p class="lead">Form binding to bootstrap modal with the Ajax Helpers<p>
</div> @Ajax.ActionLink("Add Person", "Person",null,
new AjaxOptions() {HttpMethod = "Get",UpdateTargetId = "modalContent", InsertionMode = InsertionMode.Replace, OnBegin = "onBegin", OnSuccess = "onSuccess",OnFailure = "onFailure",OnComplete = "onComplete"},
new { id = "btnPerson", @class = "btn btn-lg btn-info" }) <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" id="modalContent">
</div>
</div>
</div> @section Scripts {
@Scripts.Render("~/bundles/jqueryval") <script type="text/javascript">
function onBegin() {
//alert('begin');
} function onSuccess() {
//alert('success');
} function onComplete() {
//alert('complete');
$('#myModal').modal('show');
} function onFailure() {
alert('fail');
}
</script>
}

index 相关代码

3.弹出模态框用partialView

<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Modal with Model & Form</h4>
</div>
<div class="modal-body">
@using (@Ajax.BeginForm(new AjaxOptions() {HttpMethod = "Post",OnSuccess = "formSuccess(data)"}))
{
@Html.AntiForgeryToken() <div class="form-horizontal">
@Html.ValidationSummary(true) <div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div> <div class="form-group">
@Html.LabelFor(model => model.BirthDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BirthDate)
@Html.ValidationMessageFor(model => model.BirthDate)
</div>
</div>
</div> <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
}
</div> <script type="text/javascript"> function formSuccess(result) {
if (result.success) {
$('#myModal').modal('hide');
location.reload();
} else {
$.each(result.errors, function(key, val) {
var container = $('span[data-valmsg-for="' + key + '"]');
container.removeClass("field-validation-valid").addClass("field-validation-error");
container.html(val[val.length - ].ErrorMessage);
});
}
} $(function () {
//allow validation framework to parse DOM
$.validator.unobtrusive.parse('form');
});
</script>

partialview

4.前段验证

需加入相关的js文件:jquery.validate.unobtrusive.min.js

view中增加相关js

$(function () {
//allow validation framework to parse DOM
$.validator.unobtrusive.parse('form');
});

5.相关演示

Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证的更多相关文章

  1. bootstrap 模态框中弹出层 input不能获得焦点且不可编辑

    bootstrap 模态框中弹出层 input不能获得焦点且不可编辑 问题描述:bs框架支持一层model层的情况下,在模态框中弹出了自定义的弹出层.发现自定义弹出层的输入框不能获得焦点且不可编辑. ...

  2. ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单

    有时候,不得不考虑到以下场景问题: 数据库表字段会频繁更改扩展,而流行的重业务的js框架过于依赖json数据接口,导致的问题是,数据库表更改 -> 数据接口更改 -> 前段框架逻辑更改.. ...

  3. ASP.NET MVC利用ajax把action的JavaScript注册到页面并执行

    相信大家在做Webform时经常会遇到在页面的后台CS文件中根据数据运行结果修改页面显示样式.显示(隐藏).或者弹出框,当时我们会用到ScriptManage或者Page来向页面注册一段js来实现页面 ...

  4. bootstrap模态框弹框后执行Ajax

    如下: editModal:模态框ID <script> $(document).ready(function() { $('#editModal').on('hidden.bs.moda ...

  5. bootstrap删除模态框弹出并询问是否删除【通用删除模态框】

    普通的询问是否删除的对话框比较low,可以利用bootstrap的模态框代替普通的对话框来实现删除. 效果: 点删除的时候弹出模态框询问是否删除,点确认的时候将需要删除的ID传到后台进行删除.  过程 ...

  6. 关于【bootstrap modal 模态框弹出瞬间消失的问题】

    前提是你没有重复引入bootstrap.js\bootstrap.min.js和modal.js.一下提供一个小例子. <button class="btnbtn-primary bt ...

  7. 去除bootstrap模态框半透明阴影

    当使用bootstrap模态框默认自带半透明阴影,如果想要去除阴影,需要怎么做呢? 今天在项目中我遇到了这个问题,想要去除模态框的阴影,试了好久都没解决.后来问同事的时候才知道,当模态框弹出后,会加上 ...

  8. Bootstrap模态框原理分析及问题解决

    最近自学了bootstrap觉得里面模板样式挺好的,就想自己实现实现,不多说了,开始进入正题了 今天就来实现bootstrap里面的模态框弹出效果 首先很简单 实现一个类似于panel的modal 1 ...

  9. js实现类bootstrap模态框动画

    在pc端开发,模态框是一个很常用的插件,之前一直用的第三方插件,比如bootstrap,jQuery的模态框插件,最近还用了elementUI的.但是会发现其实动画效果都差不多,那么如何去实现这样一个 ...

随机推荐

  1. WINDOWS+L组合键锁定XP

    在Windows XP时工作时,我们经常要锁定计算机,当计算机被锁定后,只有重新登录才能够使用计算机,从而保证了计算机的安全. WINDOWS+L组合键锁定XP,就是键盘上右边的小窗口+L键 ,可以快 ...

  2. 详解Linux配置iSCSI方法

    iSCSI技术是在2001年初由IBM及Cisco联合制定的技术,在2003年5月微软在 Windows 2003中 开始自己正式支持iSCSI微软此举很大程度上的推动了iSCSI技术的发展.下面为大 ...

  3. BZOJ2083: [Poi2010]Intelligence test

    2083: [Poi2010]Intelligence test Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 241  Solved: 96[Sub ...

  4. -_-#【JS】隐含全局变量

    隐含全局变量与明确定义的全局变量有细微的不同,不同之处在于能否使用delete操作符撤销变量 使用var创建的全局变量(这类变量在函数外部创建)不能删除不使用var创建的隐含全局变量(尽管它是在函数内 ...

  5. del重复数

    楼主 发表于: 2010-06-21 11:46:31 本帖最后由 luckycynthia 于 2010-06-21 11:47:46 编辑 在抓取数据后对数据进行操作的途中,有时候会碰到重复数据, ...

  6. Bzoj 1222: [HNOI2001]产品加工 动态规划

    1222: [HNOI2001]产品加工 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 486  Solved: 298[Submit][Status ...

  7. CentOS让root用户可以SSH登录

    一.说明     Solaris 10 出于安全原因,默认参数很严格,禁止root用户直接使用ssh登陆 二.处理     1.可以先用非root的帐户,登陆到ssh后,su成root     2.如 ...

  8. 关于开发环境 git 重新部署

    apps  开发机器 多次因为升级出现无法登陆 下面就重新部署 流程做笔记 1   备份 根目录下的 那一堆shell 和 Cache/data 下的系统配置 2  shell : su www   ...

  9. wdlinux mysql innodb的安装

    mysql innodb的安装 wget -c http://down.wdlinux.cn/in/mysql_innodb_ins.sh chmod 755 mysql_innodb_ins.sh ...

  10. [RxJS] Transformation operator: repeat

    Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson ...