一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - ASP.NET MVC下Ajax.BeginForm方式無刷新提交表單實例

ASP.NET MVC下Ajax.BeginForm方式無刷新提交表單實例

2020-05-22 12:33潛龍在淵 ASP.NET教程

下面小編就為大家分享一篇ASP.NET MVC下Ajax.BeginForm方式無刷新提交表單實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

有時候,不得不考慮到以下場景問題:

數據庫表字段會頻繁更改擴展,而流行的重業務的js框架過于依賴json數據接口,導致的問題是,數據庫表更改 -> 數據接口更改 -> 前段框架邏輯更改。。。

一不小心就陷入坑坑洼洼。

這樣的話,原來純ASP.NET MVC綁定的方式,還是可以一用的,因為該方式不用再為那么多js代碼煩惱。

不好意思,前面自說自話啊,直接上干貨代碼了————

Ajax.BeginForm

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
@{
 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庫————

?
1
2
3
4
5
6
7
8
9
10
<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="~/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>

后端代碼————

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[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方式無刷新提交表單實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/kdf123/article/details/65028641

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片资源站 | 日韩欧美一区二区三区四区 | 国产精品久久久久久久久99热 | 娇女的呻吟亲女禁忌h16 | 午夜影视免费 | 高清不卡日本v在线二区 | 国内精品在线播放 | 国产视频在线一区 | 免费特黄一区二区三区视频一 | 精品精品国产yyy5857香蕉 | 国产精品久久久久久影视 | 欧美极品brazzers 高清 | 亚洲国产成人精品无码区99 | xxxxxx国产精品视频 | 亚洲天堂在线视频播放 | 成人看的羞羞视频免费观看 | 小小水蜜桃免费影院 | 欧美三级不卡在线观线看高清 | 欧亚精品一区二区三区 | 亚洲AV久久久久久久无码 | 国产成人h综合亚洲欧美在线 | 青草视频免费观看 | 国产欧美国产综合第一区 | 国产成人看片免费视频观看 | h视频免费高清在线观看 | 欧美日韩1区 | 日本动漫啪啪动画片mv | 日本老师xxxxx18 | 麻豆婷婷| 日本亚洲欧洲高清有码在线播放 | 亚洲国产剧情中文视频在线 | 男人的天堂久久精品激情 | 日本高清色视频www 日本高清免费观看 | 欧美破苞合集 magnet | 美女张开腿黄网站免费精品动漫 | 国产精品香蕉一区二区三区 | h玉足嫩脚嗯啊白丝 | 国产探花视频在线观看 | 操到翻白眼 | 日b在线 | 日本护士xxxx视频 |