Silverlight 和 MVC:将对象发布到控制器方法
我有一个 MVC 项目,其中控制器操作返回一些 JSON 数据(即通过 /Home/GetData URL)。此操作还采用自定义对象作为参数。
该操作的签名是 JsonResult GetData (MyCustomObject o)
我还有一个客户端 Silverlight 项目,在其中构建 MyCustomObject 并尝试通过 HttpWebRequest 调用此 URL (/Home/GetData/)。但是,我无法弄清楚如何在此通话中发布我的对象。我是否需要将其序列化为 Json 才能将其传入?
非常感谢!
I have an MVC project in which a controller action returns some JSON data (i.e. via /Home/GetData URL). This action also takes a custom object as a param.
This signature for the action is JsonResult GetData (MyCustomObject o)
I also have a client Silverlight project in which I'm constructing MyCustomObject and trying to call this URL (/Home/GetData/) via HttpWebRequest. However, I'm having trouble figuring out how to post in my object in this call. Do I need to serialize it to Json in order to pass it in?
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MVC 可以接受提交的数据并将其绑定到您的
MyCustomObject
对象,无论它是否以 JSON、XML、查询字符串、标准表单 POST 等形式提交。MVC不要求对象是以特定方式提交。作为设计师,您可以根据您的所有要求来确定在特定情况下最有效的方法。
提交后,MVC 将使用适合所提交数据形式的
ValueProvider
,并且DefaultModelBinder
将尝试使用 ValueProvider 中的值来绑定到您的模型。MVC can accept and bind the submitted data to your
MyCustomObject
object, regardless of whether it is submitted as JSON, XML, a query string, a standard form POST, etc.MVC does not require the object to be submitted in a particular fashion. That is up to you as the designer to determine what works best under the particular circumstances, given all of your requirements.
When submitted, MVC will use the
ValueProvider
suitable to the form of the data submitted, and theDefaultModelBinder
will attempt to use the values in the ValueProvider to bind to your model.感谢您的帮助!由于 Web 应用程序通过 getJSON 调用处理此问题,因此我最终将对象作为查询字符串参数发布,即我正在向 http://../controller/action/view.aspx?custObject 发出 Web 请求。 property1=&custObject.property2=等
Thanks for your help! Since the web app handles this with a getJSON call, I ended posting the object as a query string param i.e. I'm making a web request to
http://../controller/action/view.aspx?custObject.property1=<value>&custObject.property2=<value>
etc