有时候,不得不考虑到以下场景问题:

数据库表字段会频繁更改扩展,而流行的重业务的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="stylesheet" type="text/css" />

    <link href="~/content/libs/fa/css/font-awesome.min.css" rel="stylesheet" type="text/css" /> 

    <link href="~/content/app/css/site.css" rel="stylesheet" type="text/css" />
<link href="~/content/app/css/bootstrap-extend.css" rel="stylesheet" type="text/css" />
<link href="~/content/app/css/bootstrap-override.css" rel="stylesheet" type="text/css" /> <script src="~/content/libs/jquery/jquery.min.js"></script>
<script src="~/content/libs/jquery/jquery.validate-vsdoc.js"></script>
<script src="~/content/libs/jquery/jquery.validate.js"></script>
<script src="~/content/libs/jquery/jquery.validate.unobtrusive.js"></script>
<script src="~/Content/libs/jquery/jquery.unobtrusive-ajax.min.js"></script>

后端代码————

[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, Value = x.Id.ToString() }).ToList();
ViewBag.Areas = AreaFacade.GetAll().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
return View(entity);
}
} [Route("~/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方式无刷新提交表单的更多相关文章

  1. iframe实现面页无刷新提交表单

    一.表单提交到了哪里? 这似乎是个无知的问题,我们都知道表单提交到服务器,java,php,asp等服务器,然后由服务器去读.那么之后呢,服务器总要返回点什么吧,要么返回 一个xml或json数据,要 ...

  2. HTML无刷新提交表单

    通常对于无刷新提交表单,我们都是运用ajax实现的.前段时间跟着老大了解到另一种无刷新提交表单的方法,是利用iframe框架实现的.现在整理出来分享给大家. 第一种: (html页面) <!DO ...

  3. 使用iframe实现页面无刷新提交表单

    iframe提交表单其实比ajax要方便一些,当然ajax也有ajax的好处,只是ajax编码处理有时有些麻烦,虽然经过转码是可以解决中文问题,但如果直接使用iframe不存这些问题了,下面来看看. ...

  4. Ajax无刷新提交表单和显示

    ajax无刷新表单提交:   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  5. 不使用ajax,无刷新提交表单

    <form action="form_action.asp" method="get" onsubmit"check_form()" ...

  6. Jquery Ajax自定义无刷新提交表单Form

    Jquery的$.ajax方法可以实现ajax调用,要设置url,post,参数等. 如果要提交现有Form需要写很多代码,何不直接将Form的提交直接转移到ajax中呢. 以前的处理方法 如Form ...

  7. 无刷新提交表单(非Ajax实现)

    HTML代码: <iframe id="fra" name="frm" style="display: none;"></ ...

  8. js无刷新提交表单

    $("#form1").attr("target", "frameFile"); $("#form1").submit( ...

  9. form表单下的button按钮会自动提交表单的问题

    form表单下的button按钮会自动提交表单的问题 2017年01月05日 18:02:44 蓝色水 阅读数:18012更多 个人分类: asp.net   form表单下的按钮在没有指定type类 ...

随机推荐

  1. cas单点登录-CAS5.1.3 overlay服务器搭建(二)

    前言    本节主要讲解怎么搭建cas服务端,并且在浏览器中使用https访问cas服务端 1.通过cas代码生成工具(https://casinitializr.herokuapp.com/),生成 ...

  2. Spark Shuffle 中 JVM 内存使用及配置内幕详情

      本课主题 JVM 內存使用架构剖析 Spark 1.6.x 和 Spark 2.x 的 JVM 剖析 Spark 1.6.x 以前 on Yarn 计算内存使用案例 Spark Unified M ...

  3. JDK1.8 重识HashMap

    摘要 JDK1.8相较于1.7对HashMap做了很大的优化,比如加入了新数据结构红黑树.Hash算法的优化和扩容的优化. 本篇结合这些区别,探索HashMap的结构实现和功能原理. 存储结构-字段 ...

  4. 【WPF】使用控件MediaElement播放视频

    需求是点击按钮后,弹出弹窗播放视频.按钮的点击事件如下. public void ShowVideo() { Window window = new Window(); window.Width = ...

  5. <黑天鹅>读书笔记

    书在这里 不要关注可能性,而要关注结果 不要迷信那些估值和行业发展研究文字,因为所有大家看得到的模型和研究报告都是有关白天鹅的,而你的命运或许实际掌握在黑天鹅那里 你不知道的事比你知道的事有意义 你可 ...

  6. 新浪微博 oauth2.0 redirect_uri_mismatch

    新浪微博开放平台出来很久了,现在才开始研究,貌似有点晚了.... 第一次折腾,总是出现这样那样的问题,即使照着别人成功的例子也是一样,这不,开始运行的时候,运行下面的例子,总是报error:redir ...

  7. 输//ip提示找不到应用程序

    输//ip提示找不到应用程序??? (未测试)试试:环境变量的 PATH 中添加 C:\Windows\system32 (未测试)试试:默认程序里----协议关联里:管理ie

  8. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

  9. 请教 JTable 里的单元格如何使得双击进入单元格后,单元格的内容处于全选中状态

    http://bbs.csdn.net/topics/390195204 ———————————————————————————————————————— java 达人, 最近在开发一个 java  ...

  10. JSONP原理及jQuery中的使用

    JSONP原理   JSON和JSONP   JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,用于在浏览器和服务器之间交换信息.   JSONP(JSON ...