告诉模型绑定MVC操作参数是JSON [英] Tell modelbinding that MVC action parameter is JSON

查看:144
本文介绍了告诉模型绑定MVC操作参数是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用上载控件将文件发送到JsonResult,但同时我还在发送JSON字符串作为第二个参数.这一切都用Content-Type:multipart/form-data;

I am using an upload control to send a file to a JsonResult, but I am also sending up a JSON string as a second parameter. This is all getting posted with the Content-Type:multipart/form-data;

[HttpPost]
public JsonResult UploadDocument(HttpPostedFileBase file, DocumentViewModel model)
{ ... }

我知道,如果将内容类型设置为application/json,则MVC可以直接绑定到视图模型,但是在这种情况下,我认为我不可能对此进行设置.

I know MVC is capable of binding directly to a viewmodel if the content type is set to application/json but I don't think it's possible for me to set that in this case.

我有什么办法让MVC自动将发布的json字符串绑定到模型上?

Is there any way for me to get MVC to automatically bind my posted json string to model?

推荐答案

由于无法更改内容类型,我发现此博客正是我所需要的.

As I was unable to change the content-type I found this blog to be exactly what i needed.

"......我们的整个请求流(数据)将不是json字符串.只有guest参数将作为json字符串提供..."

"... our whole request stream(data) won’t be json string. Only the guest parameter will be supplied as json string..."

http: //ishwor.cyberbudsonline.com/2012/07/fun-with-aspnet-mvc-3-custom-json-model-binder.html

这篇关于告诉模型绑定MVC操作参数是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆