ajax.beginform bootstrap 上传,实现bootstrap模态框弹出:Asp.net MVC利用Ajax.BeginForm实现,并进行前端验证...

实现bootstrap模态框弹出:Asp.net MVC利用Ajax.BeginForm实现,并进行前端验证

1.新建Controllerpublic 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");

}

2.新建相应的index

@ViewBag.Title

Form binding to bootstrap modal with the Ajax Helpers

@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" })

@section Scripts {

@Scripts.Render("~/bundles/jqueryval")

function onBegin() {

//alert('begin');

}

function onSuccess() {

//alert('success');

}

function onComplete() {

//alert('complete');

$('#myModal').modal('show');

}

function onFailure() {

alert('fail');

}

}

index 相关代码

3.弹出模态框用partialView _Person

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 - 1].ErrorMessage);

});

}

}

$(function () {

//allow validation framework to parse DOM

$.validator.unobtrusive.parse('form');

});

partialview

4.前段验证

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

view中增加相关js$(function () {         //allow validation framework to parse DOM

$.validator.unobtrusive.parse('form');

});

Modelpublic class PersonValidationViewModel

{

[StringLength(10,MinimumLength=2)]

public string FirstName { get; set; }

public string LastName { get; set; }

[DataType(DataType.Date)]

public string BirthDate { get; set; }

}

在Views的多级文件夹中,我们在Controlls文件夹下新建了文件夹,那么对于partialView怎么处理呢?在admin_routing方法中添加如下代码PartialViewLocationFormats = new[]

{

"~/Views/Shared/{0}.cshtml",                 "~/Views/{1}/{0}.cshtml",                "~/Views/Admin/{1}/{0}.cshtml"//自定义QMRisk的视图

};

即可在Views/Admin/WebUser文件夹下添加PartialView _Person

效果图

4c1d78208f8e5ef4a1301c9d41f436cc.png

欢迎来到技术之家,

如需转载,烦请保留本文链接和出处:http://www.jszja.com/contents/13/660.html

您的支持将是我们前进的动力!如对本篇文章有疑问或建议,请通过本站下方邮箱联系我们,让技术之家每天进步一点点!(●'◡'●)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值