1. 程式人生 > >mvc Html.BeginForm和Ajax.BeginFrom表單提交

mvc Html.BeginForm和Ajax.BeginFrom表單提交

info tpm jquery log 提交 data 天使 posted ()

今天使用異步提交附件後臺死活獲取不到文件,代碼還原

技術分享圖片
 1  @using (Ajax.BeginForm("Add", "Event",  new AjaxOptions() { HttpMethod = "Post", OnSuccess = "EntryFormJS.SubmitSuccess" }, new { id = "myform" }))
 2                 {
 3                     <div class="form-group">
 4                         <label for
="name">標題</label> 5 <input type="text" class="form-control" id="title" name="title" 6 placeholder="請輸入標題"> 7 </div> 8 <div class="form-group"> 9 <
label for="inputfile">封面圖</label> 10 <input type="file" id="coverPic" name="coverPic"> 11 </div> 12 <div class="form-group"> 13 <div class="form-group"> 14 <
label for="name">內容</label> 15 <textarea id="content" name="content" style="width:100%"></textarea> 16 </div> 17 </div> 18 <input type="hidden" id="id" name="id" /> 19 <div> 20 <button type="submit" class="btn btn-success" style="margin-left:200px">提交</button> 21 </div> 22 }
View Code

控制器使用 HttpPostedFileBase postedFile = Request.Files["coverPic"];接收數據,postedFile 值總是null。大腦暫時短路了,確實ajax提交表單中如果含有文件則無法獲取

後嘗試使用jquery-form.js提交表單,後臺獲取到了附件。代碼如下

html

技術分享圖片
 1  @using (Html.BeginForm("Add", "Event", FormMethod.Post, new { enctype = "multipart/form-data" }))
 2                 {
 3                     <div class="form-group">
 4                         <label for="name">標題</label>
 5                         <input type="text" class="form-control" id="title" name="title"
 6                                placeholder="請輸入標題">
 7                     </div>
 8                     <div class="form-group">
 9                         <label for="inputfile">封面圖</label>
10                         <input type="file" id="coverPic" name="coverPic">
11                     </div>
12                     <div class="form-group">
13                         <div class="form-group">
14                             <label for="name">內容</label>
15                             <textarea id="content" name="content" style="width:100%"></textarea>
16                         </div>
17                     </div>
18                     <input type="hidden" id="id" name="id" />
19                     <div>
20                         <button type="button" onclick="submitData()" class="btn btn-success" style="margin-left:200px">提交</button>
21                     </div>
22                 }
View Code

js

技術分享圖片
 1  var submitData = function () {
 2             var content = $("#title").val();
 3             if (content.trim().length == 0) {
 4                 $.notify({ message: "請輸入標題!", status: ‘warning‘, timeout: 2000 }).show();
 5                 return false;
 6             }
 7             $(‘form‘).ajaxSubmit({
 8                 type: "post",
 9                 url: "/Event/Add",
10                 success: function (result) {
11                     if (!result.error) {
12                         $.notify({ message: "操作成功", status: ‘success‘, timeout: 2000 }).show();
13                         $(‘#eventModal‘).modal(‘hide‘)
14                         $("#tableList").bootstrapTable(‘refresh‘);
15                     }
16                     else
17                         $.notify({ message: result.msg, status: ‘warning‘, timeout: 2000 }).show();
18                 }
19             });
20         }
View Code

mvc Html.BeginForm和Ajax.BeginFrom表單提交