您现在的位置是:网站首页> 编程资料编程资料
ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例_实用技巧_
2023-05-24
262人已围观
简介 ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例_实用技巧_
有时候,不得不考虑到以下场景问题:
数据库表字段会频繁更改扩展,而流行的重业务的js框架过于依赖json数据接口,导致的问题是,数据库表更改 -> 数据接口更改 -> 前段框架逻辑更改。。。
一不小心就陷入坑坑洼洼。
这样的话,原来纯ASP.NET MVC绑定的方式,还是可以一用的,因为该方式不用再为那么多js代码烦恼。
不好意思,前面自说自话啊,直接上干货代码了————
Ajax.BeginForm
@{ Layout = null; var ajaxOptions = new AjaxOptions { UpdateTargetId = "updateHolder", OnBegin = "DeliverableEdit.onBegin", OnFailure = "DeliverableEdit.onFailure", OnSuccess = "DeliverableEdit.onSuccess", OnComplete = "DeliverableEdit.onComplete", HttpMethod = "Post" }; } @using ( Ajax.BeginForm("Save", "DesignDeliverable", null, ajaxOptions, new { @class = "form-horizontal", id = "editForm" }) ) { @Html.HiddenFor(x => x.Id) @Html.HiddenFor(x => x.TaskCode) @Html.HiddenFor(x => x.ShortName) @Html.TextBoxFor(x => x.Name, new { @class = "form-control", placeholder = "Title" }) @Html.ValidationMessageFor(x => x.Name) @Html.DropDownListFor(x => x.DeliverableType, new List { new SelectListItem { Text = "Type 1", Value = "1" }, new SelectListItem { Text = "Type 2", Value = "2" }, new SelectListItem { Text = "Type 3", Value = "3" }, new SelectListItem { Text = "Type 4", Value = "4" }, new SelectListItem { Text = "Type 5", Value = "5" }, }, new { @class = "form-control" }) @Html.TextAreaFor(x => x.Description, new { @class = "form-control", rows = 4 }) Weight @Html.DropDownListFor(x => x.Phase, new List { new SelectListItem { Text = "Phase 1", Value = "1" }, new SelectListItem { Text = "Phase 2", Value = "2" }, new SelectListItem { Text = "Phase 3", Value = "3" }, new SelectListItem { Text = "Phase 4", Value = "4" }, new SelectListItem { Text = "Phase 5", Value = "5" }, }, new { @class = "form-control" }) @Html.TextBoxFor(x => x.Detail, new { @class = "form-control", placeholder = "Detail" }) @Html.ValidationMessageFor(x => x.Detail) @Html.TextBoxFor(x => x.Size, new { @class = "form-control", placeholder = "Size" }) @Html.TextBoxFor(x => x.WbsNumber, new { @class = "form-control", placeholder = "WBS Code" }) @Html.DropDownListFor(x => x.RoomId, (ViewBag.Rooms as List), new { @class = "form-control" }) @Html.DropDownListFor(x => x.AreaId, (ViewBag.Areas as List), new { @class = "form-control" }) File Name Revision Date P-001.pdf 01 03/15/2017
} 记得引用对应的js库————
后端代码————
[Route("~/DesignDeliverable/Edit/{id?}")] [HttpGet] public ActionResult Edit(Guid? id) { using ( EmpsDbContext db = new EmpsDbContext() ) { DesignDeliverable entity = null; if ( id.HasValue ) { entity = db.DesignDeliverables.SingleOrDefault(x => x.Id == id.Value); } else { entity = new DesignDeliverable(); } ViewBag.Rooms = RoomFacade.GetAll().Select(x => new SelectListItem { Text = x.Name, Va
相关内容
- WebAPI 实现前后端分离的示例_实用技巧_
- asp.net使用H5新特性实现异步上传的示例_实用技巧_
- http转https的实战记录(iis 7.5)_实用技巧_
- .NET Core中依赖注入AutoMapper的方法示例_实用技巧_
- .NET连接数据库以及基本的增删改查操作教程_实用技巧_
- .Net整合Json实现REST服务客户端的方法详解_实用技巧_
- Visual Studio 2013+OpenCV2.4.10环境搭建教程_实用技巧_
- 利用Service Fabric承载eShop On Containers的实现方法_实用技巧_
- ASP.NET 谨用 async/await_实用技巧_
- 浅谈ASP.NET MVC 防止跨站请求伪造(CSRF)攻击的实现方法_实用技巧_
点击排行
本栏推荐
