有时候,不得不考虑到以下场景问题:
数据库表字段会频繁更改扩展,而流行的重业务的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) <div class="container-fluid pad-15"> <div class="row"> <div class="col-xs-6"> <div id="updateHolder" style="display:none;"></div> <div class="form-group"> <label class="col-sm-2 control-label">Title</label> <div class="col-sm-4"> @Html.TextBoxFor(x => x.Name, new { @class = "form-control", placeholder = "Title" }) @Html.ValidationMessageFor(x => x.Name) </div> <label class="col-sm-2 control-label">Type</label> <div class="col-sm-4"> @Html.DropDownListFor(x => x.DeliverableType, new List<SelectListItem> { 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" }) </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Description</label> <div class="col-sm-10"> @Html.TextAreaFor(x => x.Description, new { @class = "form-control", rows = 4 }) </div> </div> <div class="form-group" style="margin-bottom: 3px;"> <div class="col-sm-2 col-sm-offset-10"> Weight </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Phase</label> <div class="col-sm-3"> @Html.DropDownListFor(x => x.Phase, new List<SelectListItem> { 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" }) </div> <label class="col-sm-2 control-label">First</label> <div class="col-sm-3"> <input class="form-control" type="text" placeholder="" /> </div> <div class="col-sm-2"> <input class="form-control" type="text" placeholder="Weight" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Detail</label> <div class="col-sm-3"> @Html.TextBoxFor(x => x.Detail, new { @class = "form-control", placeholder = "Detail" }) @Html.ValidationMessageFor(x => x.Detail) </div> <label class="col-sm-2 control-label">Second</label> <div class="col-sm-3"> <input class="form-control" type="text" placeholder="" /> </div> <div class="col-sm-2"> <input class="form-control" type="text" placeholder="Weight" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Size</label> <div class="col-sm-3"> @Html.TextBoxFor(x => x.Size, new { @class = "form-control", placeholder = "Size" }) </div> <label class="col-sm-2 control-label">Third</label> <div class="col-sm-3"> <input class="form-control" type="text" placeholder="" /> </div> <div class="col-sm-2"> <input class="form-control" type="text" placeholder="Weight" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">WBS Code</label> <div class="col-sm-3"> @Html.TextBoxFor(x => x.WbsNumber, new { @class = "form-control", placeholder = "WBS Code" }) </div> <label class="col-sm-2 control-label">Fourth</label> <div class="col-sm-3"> <input class="form-control" type="text" placeholder="" /> </div> <div class="col-sm-2"> <input class="form-control" type="text" placeholder="Weight" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Room</label> <div class="col-sm-3"> @Html.DropDownListFor(x => x.RoomId, (ViewBag.Rooms as List<SelectListItem>), new { @class = "form-control" }) </div> <label class="col-sm-2 control-label">A Variance</label> <div class="col-sm-3"> <input class="form-control" type="text" placeholder="A Variance" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Area</label> <div class="col-sm-3"> @Html.DropDownListFor(x => x.AreaId, (ViewBag.Areas as List<SelectListItem>), new { @class = "form-control" }) </div> <label class="col-sm-2 control-label">B Variance</label> <div class="col-sm-3"> <input class="form-control" type="text" placeholder="B Variance" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">Comments</label> <div class="col-sm-10"> <textarea class="form-control" rows="4"></textarea> </div> </div> </div> <div class="col-xs-6"> <div class="form-group"> <div class="col-sm-12"> <label class="control-label">Documents</label> <table class="table table-bordered table-hover table-condensed mt-10"> <thead> <tr> <th>File Name</th> <th>Revision</th> <th>Date</th> </tr> </thead> <tbody> <tr> <td>P-001.pdf</td> <td>01</td> <td>03/15/2017</td> </tr> </tbody> </table> </div> </div> </div> </div> </div> <div class="page_footer absolute-position"> <div class="page_footer_inner page_footer_light text-right"> @if ( Request["action"] != "View" ) { <button class="btn btn-primary" id="btnSave"><i class="fa fa-floppy-o fa-fw"></i> Save</button> } <button id="btnCancel" class="btn btn-default"><i class="fa fa-close fa-fw"></i> Close</button> </div> <span id="notification"></span> </div> } <script type="text/javascript"> var DeliverableEdit = DeliverableEdit || {}; (function (o) { o.timer = null; o.displayLoading = function (target) { var element = $(target); kendo.ui.progress(element, true); o.timer = setTimeout(function () { kendo.ui.progress(element, false); }, 50); }; o.hideLoading = function (target) { setTimeout(function () { clearTimeout(o.timer); }, 50); }; o.initializeValidation = function () { $.validator.setDefaults({ showErrors: function (errorMap, errorList) { $(".page_footer_inner button").removeProp("disabled", "disabled"); // Clean up any tooltips for valid elements $.each(this.validElements(), function (index, element) { var $element = $(element); $element.data("title", "") // Clear the title - there is no error associated anymore .removeClass("field-validation-error") .tooltip("destroy"); }); // Create new tooltips for invalid elements $.each(errorList, function (index, error) { var $element = $(error.element); $element.tooltip("destroy") // Destroy any pre-existing tooltip so we can repopulate with new tooltip content .data("title", error.message) .data("placement", "bottom") .addClass("field-validation-error") .tooltip(); // Create a new tooltip based on the error messsage we just set in the title }); } }); $.validator.unobtrusive.parse($("#editForm")); }; o.showSuccess = function (msg) { $("#notification").data('kendoNotification').show(msg, "success"); }; o.showWarning = function (msg) { $("#notification").data('kendoNotification').show(msg, "warning"); }; // Events during the submit of Ajax.Form o.onBegin = function () { $(".page_footer_inner button").prop("disabled", "disabled"); o.displayLoading($(".form-horizontal")); } o.onSuccess = function (data) { o.hideLoading(o.timer); if (!data || !data.code) { o.showWarning("Failure,please try it again."); return; } if (data && data.code) { gridView.refreshGrid(); o.showSuccess("Saved successfully."); setTimeout(function () { gridView.closeDeliverableDialog(); }, 500); } } o.onFailure = function (request, error) { o.hideLoading(o.timer); o.showWarning("Failure,please try it again."); } o.onComplete = function (request, status) { o.hideLoading(o.timer); $(".page_footer_inner button").removeProp("disabled", "disabled"); } })(DeliverableEdit); $(function () { // To fix jquery.validation invalid issue DeliverableEdit.initializeValidation(); $("#btnCancel").click(function (e) { e.preventDefault(); gridView.closeDeliverableDialog(); }); $("#btnSave").click(function (e) { e.preventDefault(); $(".form-horizontal").submit(); $(".page_footer_inner button").prop("disabled", "disabled"); }); $("#notification").kendoNotification({ position: { pinned: true, top: 15, left: '50%' }, autoHideAfter: 1000 }); }); </script>
记得引用对应的js库————
<link href="~/content/libs/bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet" type="text/css" /> <link href="~/content/libs/fa/css/font-awesome.min.css" rel="external nofollow" rel="stylesheet" type="text/css" /> <link href="~/content/app/css/site.css" rel="external nofollow" rel="stylesheet" type="text/css" /> <link href="~/content/app/css/bootstrap-extend.css" rel="external nofollow" rel="stylesheet" type="text/css" /> <link href="~/content/app/css/bootstrap-override.css" rel="external nofollow" rel="stylesheet" type="text/css" /> <script src="/UploadFiles/2021-04-02/jquery.min.js">后端代码————
[Route("~/DesignDeliverable/Edit/{id")] [HttpGet] public ActionResult Edit(Guid"~/DesignDeliverable/Save")] [HttpPost] public JsonResult Edit(DesignDeliverable model) { using ( EmpsDbContext db = new EmpsDbContext() ) { if ( !ModelState.IsValid ) { return Error(); } try { model.GroupId = new Guid("e030c300-849c-4def-807e-a5b35cf144a8"); //todo: fix this hardcode db.DesignDeliverables.AddOrUpdate(model); db.SaveChanges(); return Success(); } catch { //TODO: to store the exception message } return Error(); } }以上这篇ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月30日
2024年11月30日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]