码迷,mamicode.com
首页 > Web开发 > 详细

MVC-前台调用后台action 传递upload file 参数问题

时间:2015-11-07 13:24:36      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 

在后台获取upload file 数量的时候发现count一直为0,经检查发现了问题 ,代码如下:

 

前台:

var data = $("#DetailForm").serialize();

$.ajax({
url: ‘@Url.Action("SaveRequest", "RegistrationRequest")‘,
type: "POST",
dataType: "JSON",
data: data,
cache: false,
processData: false,
success: function (result) {
window.scrollTo(0, 0);
utility.hideMask();
FormSaveSuccess(result);
}
});

 

后台:

[HttpPost]
public ActionResult SaveRequest(DetailModel model)
{
   if (ModelState.IsValid)
   {

     if (model.Type == Codes.VT.ToString() || model.Type == Codes.NT.ToString())
     {
     model.DocumentsList = Utility.BuildDocumentModel(Request.Files, model.DocumentSearchCriteria.ModuleCode,  "", "",  model.DocumentSearchCriteria.FunctionId);

     if (model.DocumentsList.Count == 0)
    {
       return Json(new { HasError = true, ErrorMessage = MessageResources._NoUploadFile });
    }

  }

}
}

Request.Files 里文件count ==0.

解决这个问题只需要在前台调用action脚本里加上 属性 mimeType: "mutipart/form-data",

 

$.ajax({
url: ‘@Url.Action("SaveRequest", "RegistrationRequest")‘,
type: "POST",
dataType: "JSON",
data: new FormData(this),

mimeType: "mutipart/form-data",

cache: false,
processData: false,
success: function (result) {
window.scrollTo(0, 0);
utility.hideMask();
FormSaveSuccess(result);
}
});

MVC-前台调用后台action 传递upload file 参数问题

标签:

原文地址:http://www.cnblogs.com/wwzhang/p/4944860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!