views:

204

answers:

2

hi, i have an Ajax ActionLink which normally just returns a PartialView (which is just a UserControl ascx file) however, my needs have changed and i want to return another PartialView (so a total of two PartialViews) that occupy different areas of my page... of course i can't call " return PartialView("UserControl.ascx") " twice in a row consecutivelly... so my question is what would be an elegant work around for this?

how can i return two PartialViews WITHOUT wrapping these two PartialViews up in a larger parent view? i hesitate to do this because both items are in a different part of the html table which would require me to include practically the entire page in the parent view due to the structure of the table, and in this case lots of html data unnecessarily would be sent to the browser at each request - defeating the purpose of an ajax call/partial update (correct me if i'm wrong).

+1  A: 

Im sorry I misread, i thought you wanted to call an action method statically. If you want to update 2 parts with one click then I dont really now how you would do it with the included apis. What you could do is create a little javascript (jquery!) that takes over the link´s click, and then have the script load the render page with ajax.

I´ll post an example in a few minutes :P

<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

        $(document).ready(function () {

              $("#TheLink").click(){        
                    $("#PlaceToUpdate1").load("/Controller/Method/View1");
                    $("#PlaceToUpdat2").load("/Controller/Method/View2");

              }

            });
        }); 
    </script>

im not sure if that will work exactly like that (no compiler, just top of my head) but its something like that. Of course the link should be a dummy link that doesnt do anything, since the script is the one actually doing it (though you can intercept the links methods if you send back a false or something like that)

Francisco Noriega
will use hackery as a last resort, hoping to find elegant solution, however, just a question, does the load method load both those calls async? or does the second line run only after the first line has fully completed?
Erx_VB.NExT.Coder
actually.. I am not sure :P ....I wonder where somebody could answer me that...
Francisco Noriega
Well SO helped with that, they would execute asynchttp://stackoverflow.com/questions/2062241/would-2-consecutive-load-calls-in-jquery-execute-async
Francisco Noriega
A: 

You could manually construct the partial views in HTML helper methods. However, the feasibility of that approach depends on whether or not the partial views will be reused in other pages. I build a data grid control from scratch using HTML helper methods. Some of my pages have three or four of these data grids managed by a jQuery accordion control to reduce the screen footprint. I chose building an HTML helper for a number of reasons. First, I expected to use this control throughout my applications. Second, I didn't want to burden my views with a bunch of conditional logic. Finally, I wanted to be able to change the grid's configuration (including the model) within the view, so I wouldn't have to recompile every time I changed it. The grid supports both LINQ-to-SQL models and user-defined classes (using reflection), has a built-in pager control and a search mechanism that supports multiple search fields in a grid. I also set up the columns so that they can either display formatted text, link to a controller action or hold a mailto: link. Within the grid itself, you can define the model to populate the grid, optionally set the columns to display, specify the action and controller for creates, and specify a JavaScript function for deletes (because I use the jQuery dialog plugin for confirmation messages). All these changes are managed in the view itself.

Learning how to leverage HTML helper methods gives you the closest thing to ASP.NET server controls that MVC provides.

Neil T.
neil, that sounds fantastic, let me ask you, are you able to update all of these views that you've generated using custom helpers when one action link is called? ie: user clicks something, and i/u need/want to update several parts of the page, in different areas... yes?
Erx_VB.NExT.Coder
Erx_VB.NExT.Coder
of course just to add, im doing all this in 100% ajax, reloading entire page is just not an option unless a user is moving to a different page.
Erx_VB.NExT.Coder
The purpose of the helpers is only to assist with the construction of the HTML view. If you're using Ajax, you're probably not going to get away from having to write a reasonable amount of JavaScript, even if it's just to call the appropriate controller action method. My data grid is not Ajax-enabled yet, because I want to make sure it's stable before I start adding the Ajax/jQuery magic.
Neil T.
so if you need to update 4 parts of your page you need to make 4 client-server calls (awch) instead of back in the dreded updatePanel days where you could do it in one call and page lifecycle! ?? oh my god, this is just poor framework design!! as usualy, VS always leaves a bad taste in my mouth once i start getting to know it, should have stuck with webforms!! :(
Erx_VB.NExT.Coder