如何讓 DotNetNuke 6.2 服務框架對 json 數據進行模型綁定 (How can I get DotNetNuke 6.2 Service Framework to modelbind json data)


問題描述

如何讓 DotNetNuke 6.2 服務框架對 json 數據進行模型綁定 (How can I get DotNetNuke 6.2 Service Framework to modelbind json data)

DotNetNuke Serviceframework is based on ASP.NET MVC 2, and therefore does not include json modelbinding out of the box.

I've tried a number of approaches:

  • MVC3 jsonvalueprovider
  • custom json model binder
  • custom value provider

The code to register these was called, however the methods on these objects themselves were not called.

Registering these is an interesting area in itself as in DotNetNuke, we don't have access to the global.asax file.

I've also attempted to deserialize the request input stream, in the the controller, but I get a nullreferenceexception there, and I get the correct data size, but all nulls!

Any ideas?!


參考解法

方法 1:

Ok,

I have a workaround that is clean and functional.

I'm using a jquery plugin from here .This converts json into a standard forms form post for you. using this with jquery & knockout looks like this:

 $.ajax({ 
       url: '<%= ModulePath %>Api/Register/Search', 
       type: 'POST',  
       data: $.toDictionary(ko.mapping.toJS($root),"",true), 
       success: function (data) { //do something });

Leaving the question open, in case anyone has any ideas to get json working directly.

方法 2:

You need to register a JSON value provider to get this to work.  See http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx for details.

The best way to register the value provider is to do it in your route mapper.  Be sure to guard the registration to ensure it occurs only once, as the route mapper is occasionally called more than once.  If you are going to do this in a module deployed to servers you don't control you should probably inspect the contents of the factories collection to ensure no other service has already registered the value provider.

Services Framework in DNN 7 is based on WebAPI and natively supports JSON, so this hassle will go away soon.

(by stevenrcfoxstevenrcfoxScottS)

參考文件

  1. How can I get DotNetNuke 6.2 Service Framework to modelbind json data (CC BY-SA 3.0/4.0)

#asp.net-mvc-2 #dotnetnuke #JSON #model-binding






相關問題

使用 c# 將日期時間轉換為日期 (Convert datetime to date using c#)

如何讓 DotNetNuke 6.2 服務框架對 json 數據進行模型綁定 (How can I get DotNetNuke 6.2 Service Framework to modelbind json data)

下拉級聯 MVC 2 (Drop Down Cascading MVC 2)

如何:(jQuery) 帶有 ASP.NET MVC 2 的模態登錄對話框? (How to: (jQuery) Modal login dialog w/ ASP.NET MVC 2?)

如何在 MVC 應用程序的 URL 中使用日期(MM-dd-yyyy 格式)? (How Do I Use a Date (in the MM-dd-yyyy format) in a URL in an MVC Application?)

如何在 asp mvc 2 中進行上傳工作? (How to make upload work in asp mvc 2?)

如何從 ViewData 為 MVC2 Html.HiddenFor 設置一個值 (How to set a value for MVC2 Html.HiddenFor from ViewData)

複雜模型驗證 (Complex model validation)

ASP.NET MVC 2:調用存儲過程,獲取多個結果集 (ASP.NET MVC 2: Calling Stored Procedure, Getting Multiple ResultSets)

JsonResult 相當於 [DataMember(Name="Test")] (JsonResult equivalent to [DataMember(Name="Test")])

ASP.NET MVC 是否適合活動票務網站? (Is ASP.NET MVC a Good Fit for an Event Ticketing Site?)

跨區通話 (Cross area calls)







留言討論