if i 开发者_Python百科have a div or a partialcontrol and i have link that i want to click that will refresh the div, it seems like i can:
- use ajax.beginform or ajax.actionlink and return PartialResult()
- use jquery to post/get/ajax and return json
if i have a big complex object that i am binding to my view, which option is better. it seems like for #1, you get the benefit of the static typing of the csharp code (versus json parsing) . . any other key decision points that should come into play ? are there any tools that makes converting deeply nested csharp objects to json from controller actions.
There are a lot of options to be selected for using to manage very complex .NET object for controller. In my opinion, I think in RIA application that has ton of complex control, it should impossible to render view or partial view of it because you must dynamically create ton of html like div of div of div to display only one complex control like gridview.
So, rendering with JSON result for controller should be the best way to handle it. Because you can control any piece code and binding data to complex control easily.
For example, Data View Control and Ajax Templates features of ASP.NET Ajax Library Beta are enable you to bind data to any template in HTML document like the following template.
<div id="CustomerView" class="sys-template">
<ul>
<li>{{ContactName}} - {{Country}}</li>
</ul>
</div>
For more information, please look at WALKTHROUGH Using a DataView with local data.
One thing that maybe you want to control is the possible app info/error messages, its difficult to tell what happen just with plain text/html, with JSON you have more controll, you can use an structure like:
{
SUCCESS: true,
DATA: "",
ERRORS: []
}
Where SUCCESS tells you the status what happened "OK/info/exceptions".
DATA is where you put the HTML, objects.. you name it, this depends on the SUCCESS, if it was TRUE Data holds the html to be replaced, if it was FALSE maybe data is empty.
ERRORS holds the exceptions messages or the feed back that you want the user to see if something went wrong with the request.
The tricky part here is the render partial as string and parse to JSON, here is an approach of how it can be done.
精彩评论