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

今天使用异步提交附件后台死活获取不到文件,代码还原

 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                 }

控制器使用   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                 }

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         }

时间: 2024-08-27 23:24:43

mvc Html.BeginForm和Ajax.BeginFrom表单提交的相关文章

ajax form表单提交 input file中的文件

ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了解决这个问题我走了不少弯路: 1.用原生的 input file , 不支持ajax上传文件,你肯定会说可以用 ajax form表单上传了呀?不过我后面还要调用上传成功后用js处理一些对话框,所以这种方法排除 2.用了 uploadify 上传插件,弄出来能上传东西,结果不理想:因为不能判断上传的

ajax的表单提交,与传送数据

ajax的表单提交 $.ajax ({ url: "<%=basePath%>resource/addPortDetectOne.action", dataType: "json", method : 'post', data: $("#form_portDetect").serialize(),              //传送表单数据 success : function(data_) { //alert(data_); ale

使用ajax处理表单提交信息

很直观的需求 1.需要在不进行页面跳转的情况下进行表单验证,或者只是单纯的数据上传 2.不使用默认的提交方法,很low!和难受 这是表单 <form id="form"> <input type="text" name="user" id="user" placeholder="user"> <br> <input type="password"

Ajax form表单提交

1. 使用 $("form").serialize() 来获取表单数据 $.ajax({ type: 'post', url: 'your url', data: $("form").serialize(), success: function(data) { // your code } }); 2. 使用jquery.form.js 插件 $('#fm').form('submit', { url: url, onSubmit: function () { re

ajax模拟表单提交,后台使用npoi实现导入操作 方式一

页面代码: <form id="form1" enctype="multipart/form-data"> <div style="float:right">   <button type="button" class="btn btn-primary" onclick="$('#fileUpload').click()" id="reviewFi

jeecg &lt;t:ckeditor ajax form表单提交 获取不到值问题解决

<t:ckeditor是jeecg里面的标签,要使用必须<t:formvalid里面使用 但是这次是要写独立的页面,跟之前的不一样,所以标签里面的内容获取不到,查看框架源代码 </textarea><script type="text/javascript">var ckeditor_goodsDetails=CKEDITOR.replace("goodsDetails_text",{filebrowserBrowseUrl:'p

input file 在开发中遇到的问题 类似ajax form表单提交 input file中的

http://www.midifan.com/moduleuser-index-428015.htmhttp://www.midifan.com/moduleuser-index-428074.htmhttp://www.midifan.com/moduleuser-index-428060.htmhttp://www.midifan.com/moduleuser-index-428282.htmhttp://www.midifan.com/moduleuser-index-428172.htm

MVC中处理表单提交的方式(Ajax+Jquery)

MVC中处理表单有很多种方法,这里说到第一种方式:Ajax+Jquery 先看下表单: <form class="row form-body form-horizontal m-t"> <div class="col-md-6"> <div class="form-group"> <label class="col-sm-3 control-label">订单编号:</la

Asp.net Mvc post表单提交多个实体模型

上一遍说道用Tuple实现Asp.net Mvc action返回多个模型实体给view,此篇发过来,实现view表单提交多个实体模型到action. 1.view代码: @{ Layout = null; } @model Tuple<Model.UserInfo, Model.UserSlave> <!DOCTYPE html> <html> <head> <title></title> <meta name='viewpo