C# 将JSON和输入文件传回控制器MVC4
我试图将JSON对象和两个输入文件字段从视图传回MVC4控制器。如果我在ajax中设置了C# 将JSON和输入文件传回控制器MVC4,c#,jquery,ajax,json,asp.net-mvc-4,C#,Jquery,Ajax,Json,Asp.net Mvc 4,我试图将JSON对象和两个输入文件字段从视图传回MVC4控制器。如果我在ajax中设置了async:true,,那么我得到的是JSON对象,没有文件。如果我将我的async:false设置为,我得到的是文件,没有JSON 你知道为什么我不能把所有东西都送回控制器吗?我正在尝试将图像上载到我的服务器 另外,作为旁注,我的JSON从不返回成功,它总是出错,但当async:true,时,值仍然传递给控制器 任何帮助都会很好,谢谢 JSON: 阿贾克斯: 控制器: [HttpPost, Validate
async:true,
,那么我得到的是JSON对象,没有文件。如果我将我的async:false设置为,
我得到的是文件,没有JSON
你知道为什么我不能把所有东西都送回控制器吗?我正在尝试将图像上载到我的服务器
另外,作为旁注,我的JSON从不返回成功,它总是出错,但当async:true,
时,值仍然传递给控制器
任何帮助都会很好,谢谢
JSON:
阿贾克斯:
控制器:
[HttpPost, ValidateInput(false)]
public ActionResult Create(AdminObjectModel jsonData,
IEnumerable<HttpPostedFileBase> images, IEnumerable<HttpPostedFileBase> small_images)
{
...
return Redirect("/admin/create");
}
private static AdminObjectModel create = new RecipeObjectModel();
[HttpPost, ValidateInput(false)]
public void CreateJSON(AdminObjectModel jsonData)
{
create = jsonData;
}
[HttpPost, ValidateInput(false)]
public ActionResult Create(IEnumerable<HttpPostedFileBase> images, IEnumerable<HttpPostedFileBase> small_images)
{
string t = create.Details.Name;
...
return Redirect("/admin/create");
}
[HttpPost,ValidateInput(false)]
公共操作结果创建(AdminObjectModel jsonData,
IEnumerable图像,IEnumerable小图像)
{
...
返回重定向(“/admin/create”);
}
HTML:
...
...
...
我想我找到了一个解决方案,不确定它是否是最好的,但它确实有效
视图>AJAX:
$.ajax({
url: '/Admin/CreateJSON',
type: 'POST',
data: JSON.stringify(json),
dataType: 'json',
contentType: "application/json; charset=utf-8",
async: false,
cache: false,
success: function () {
console.log('SUCCESS');
},
complete: function () {
console.log('COMPLETE');
}
});
查看>HTML:
<form action="/Admin/Create" enctype="multipart/form-data" method="post">
...
<input type="file" id="images" class="span6" name="images" multiple />
...
<input type="file" id="small-images" class="span6" name="small_images" />
...
</form>
...
...
...
控制器:
[HttpPost, ValidateInput(false)]
public ActionResult Create(AdminObjectModel jsonData,
IEnumerable<HttpPostedFileBase> images, IEnumerable<HttpPostedFileBase> small_images)
{
...
return Redirect("/admin/create");
}
private static AdminObjectModel create = new RecipeObjectModel();
[HttpPost, ValidateInput(false)]
public void CreateJSON(AdminObjectModel jsonData)
{
create = jsonData;
}
[HttpPost, ValidateInput(false)]
public ActionResult Create(IEnumerable<HttpPostedFileBase> images, IEnumerable<HttpPostedFileBase> small_images)
{
string t = create.Details.Name;
...
return Redirect("/admin/create");
}
private static AdminObjectModel create=new RecipeObject Model();
[HttpPost,ValidateInput(false)]
public void CreateJSON(AdminObjectModel jsonData)
{
create=jsonData;
}
[HttpPost,ValidateInput(false)]
公共操作结果创建(IEnumerable图像、IEnumerable小图像)
{
字符串t=create.Details.Name;
...
返回重定向(“/admin/create”);
}
它总是返回错误而不是成功,因为您告诉ajax调用希望通过说dataType:'json',
返回json数据。它会命中错误处理程序,因为您正在执行重定向,而不是传回JSON
private static AdminObjectModel create = new RecipeObjectModel();
[HttpPost, ValidateInput(false)]
public void CreateJSON(AdminObjectModel jsonData)
{
create = jsonData;
}
[HttpPost, ValidateInput(false)]
public ActionResult Create(IEnumerable<HttpPostedFileBase> images, IEnumerable<HttpPostedFileBase> small_images)
{
string t = create.Details.Name;
...
return Redirect("/admin/create");
}