Refresh several partial view - asp.net-mvc

Is there a way to refresh several partial view from the controller ? (return View())
Thanks,
Update1:
Example, the content pârt of my screen is divided in 2 parts, on the left a customer list on the right the details customer, the details of the customers selected in the list of the left. If I create an new customer, when I save, I'd lile refresh the list (left part) and see the details (right part)

Kris,
the only way i can think to do this simply is by having multiple partialviews embeded within the main 'view' to be refreshed. these would then be refreshed in the same cycle. alternatively, you could have custom html helpers embedded within the main view that ran approprite code when the view was refreshed.
As for multiple views from a single action, i don't think this is both a good idea or in any way possible.
of course, rules are there to be broken :)

I don't think there is any automatic way to do this, but you could use some convention and:
Create a custom view result that takes in multiple partial view results i.e. a MultiplePartialViewResult
In the execute of the custom view result, call each the execute method of each of the supplied views'. Make sure to wrap each in a div or some other container for ease of retrieval at the client script
Upon receiving the response on the AJAX call in the client script, grab the value from each container and replace it in the corresponding elements matching the partial views initially rendered
For the last step you could use a convention. A simple one would be (if there is only one instance for each partial view) to put the id of target html element to update in the div/container you used to wrap it in the second step.

Based on what you're saying, I think using javascript and ajax to refresh from the server would be best.
You could use Html.RenderPartialAction to achieve DRY by putting it on the page, and then loading it using ajax and javascript.
If you were using jQuery, then something like this would work:
jQuery("#divToReload1").load('Url/To/PartialAction')...
jQuery("#divToReload2").load('Url/To/PartialAction')...
Just put that all inside one function and you'll reload all your partials at once.
You can send data through using the data parameter and catch the callback function to do as you wish.

Related

Can I have a MVC partial/child view which is NOT rendered in a deferred manner?

From what I can tell, MVC seems to treat partial views differently than the main views. It seems as though I can access an object within the main view, but not its child views if that object is going to be disposed once the main controller action returns (see my question here).
I would assume that this is to facilitate async updating of those partial views via AJAX or some other method long after an object in the main controller action is disposed. This makes perfect sense to me.
The way I'm trying to use a partial view right now, I don't need this async updating capability. I COULD just cut out the partial view all together and put it all together, but what I'm looking for is just a way to segment out my views into smaller chunks, rather than having them all in one huge file.
Ideally, I'd like to be able to pass an object from my controller to my view, utilize that object in the view AND any child/sub views, and dispose of the object only after the view and its sub/child views are finished using it.
EDIT - Adding an example
I have a Big Complicated View in a single file. Imagine that each line is actually a larger, more complicated block of view markup and code:
ViewStuff.cshtml
ViewStuff
ViewStuff
MoreViewStuff
MoreViewStuff
MoreViewStuff
ViewStuff
EvenMoreViewStuff
EvenMoreViewStuff
EvenMoreViewStuff
ViewStuff
I would like to break this view into some separate files for convenience and maintenance, effectively extracting portions of it to sub views, like an "extract method" refactoring sort of:
ViewStuff.cshtml
ViewStuff
ViewStuff
RenderPartialViewOf MoreViewStuff
ViewStuff
RenderPartialViewOf EvenMoreViewStuff
ViewStuff
MoreViewStuff.cshtml
MoreViewStuff
MoreViewStuff
MoreViewStuff
EvenMoreViewStuff.cshtml
EvenMoreViewStuff
EvenMoreViewStuff
EvenMoreViewStuff
From what I can tell, partial views seem to be the answer to this, although there seem to be a lot of methods one could use. The problem I'm getting is that my controller is passing in an object to the view which is disposed when the controller action returns.
In the "all in one file" scenario, I can successfully utilize the object to construct the full view, because the whole view is constructed before the method returns and the object is disposed. However, this is not ideal because it means large, long, monolithic view files and potential duplication of portions later on.
In the "multiple files" scenario, the main view can access the object fine, because it hasn't been disposed yet. But the main controller action method returns, disposing the object, before the partial views have gotten it. The result is an error in the partial view, looking for an object which is no longer available.
What I want is a way to construct a complete view made from multiple organized chunks, all before the controller method disposes of my object.
Again, see my other question for some more specific code regarding what I'm doing with this object, the using statement it's in within the controller, etc.
Your problem has nothing to do with Partials. I've already addressed the issue in your other question. The problem also has nothing to do with async, as it's been this way since the very first version of MVC, which did not support async.
You have to pass objects to the view which are not disposed when the method returns.
It seems you're just confused about the difference between partials and child actions.
A partial view is some HTML without a layout. It will inherit the model of it's parent view, unless you pass a different model. This is most likely what you need to use:
#Html.Partial("_MyPartialView")
That's literally all you need. You don't need to add anything at all to your controller.
Now, child actions are different. They're like normal actions, but run within the main request. In other words, they're not accessed through a URL like a normal action would be, but they are nonetheless, completely separate from the main request. They also function as GETs. You don't POST to a child action, which means you can't send along complex data types like an entire model. Their purpose is to allow you render something that doesn't really fit in with the scope of the rest of the page. For example, if your main action and view are tasked with fetching an rendering a single blog post, you might still want to have a side bar that shows other recent posts. With a child action you can fetch the list of recent posts and render that within a view that has its model set for a list of posts, all without affecting the main view.

JSF 2.0 /CDI Scopes and Best Practises

let's assume i have the following structure:
pageA.xhtml - Here we can select an item which will be needed within pageB and pageC but not in pageE.
pageB.xhtml - Here we use the Item which was selected from pageA. We
also have a selectBox and some Buttons on this page.
When selecting something from the selectBox some Buttons will be deactivated and some Text can be displayed.
(when refreshing this page we want the same state again). pageB includes
pageD which lists some stuff. Now we can navigate to pageC.
We also create some objects which are only relevant for pageC but not for other pages.
pageC.xhtml - here we get the object from pageB and depending on some User input we modify it and when we press apply we come back
to pageB which displays
our changes. From pageB we can press save which will save the changes and pageD (which is included in pageB) will be
updated.
pageD.xhtml - just lists some stuff. (will only included within pageB)
pageE.xhtml - This page will start something completely differend and does not need the input from pageA but you can navigate directly
to pageC. In this case pageC has to
hide some things.
I hope the example is somehow clear. Actually i just made it up to make my question a bit clearer: I want to know what the best practises are to pass data between different pages and save the actual state (also have the same state when coming back).
Also how to reset/clear data which are needed in some pages but not in different ones.
For example some data will be needed for several pages but some only within nested pages (in an optimal world the data within the nested pages should be cleared when leaving them)
Of course i could save stuff i need into the session, but then i have to be careful to remove those stuff again when i don't need it anymore. JSF and CDI support Conversations. But the problem here is that it is not possible to have nested conversations. Of course i also could pass everything with request parameters .. but in this case i have to be careful if i have ajax requests within my page (i guess i would have to send always all parameters).
I'm using JSF 2.0 with CDI. Any answer will be appreciated. Sadly i cannot provide any code example .. so i hope i was able to express my self clear enough.
greetings kukudas
You could create a new CDI scope or recreate the ViewScope in CDI. Take a look at CODI conversations as well.

Telerik Grid MVC and check all checkbox on all pages

On Telerik demo site we can see an example of how to implement kind of functionality: "check all checkbox in a grid's column". But in my case it has 2 disadvantages:
It didn't check all checkbox on all pages.
It didn't save a state of checkboxes on different pages.
Is anybody know how to resolve these issues? Thanks in advance.
As long as I know there's no built-in functionality to do so. The same problem happens when you select records on page one and change to page two, you loose whatever you selected before.
To achieve that functionality you have 2 options (I've used both on previous projects)
1) On each check make an Ajax call to one of your controllers and store whatever you selected on a Session Variable (This can be inefficient if you have a lot of records)
2) Create a javascript variable and store your selections there, and send back to the controller using a json variable or a comma separated values string
As I said, I've used both approachs so it depends on if this works for you or not
Hope it helps
I can't test this, so I'm not 100% sure, but looking at Telerik's example, one reason it's not persisted is because every "page" of the grid requires a postback, and in the controller action result method, they aren't passing in the model (or view model) for the items that are bound to the grid, they're only returning that list of items back to the view, so it will never "save" which items are checked/selected and which ones aren't. You should be able to get around this by making your view model a parameter into the HttpPost action result method and then passing that list back to the view after the post so that it retains which items are selected instead of creating a new one. This won't solve the issue with not selecting all the items, but it should at least retain which ones are selected throughout the pages. I think the reason for it not working with all items is it can only select the ones that are actually being displayed at the time. You may want to do a post (or ajax) to select "all" items.
One of the major reasons for using paging in grids is so that you don't have to retrieve all of the data from the data store and generate a lot of HTML to push to the client.
It's been my experience that most users understand that a "select all" check box only checks the items on the current page. I've not seen a site where checking such a check box would actually check all records, even those I can't see.
If you have an action which will affect more than the current page of records, I would suggest that you add a button which clearly indicates that the action will affect all records, then send a command to your data layer which will perform that action. This will perform better (you don't have to send a potentially long list of ids across the wire) and allow users to understand the repercussions of their action.

multi action rails view

I have an Controller X which which has an action new.
As part of the creation process, I would like to handle multiple things before the form is submitted.
Three operations I would like to do is Search, Evaluate and Create.
So when the view is first loaded, it will have the form to input some fields. Based on the input, I need to call some ruby utils to build the "data" object and then display those results back to the page. I think I have this part figured out, I have a form_for('/x/new') tag in my view and I check in my controller if request.post? then calculate data object.
But when the search is done and the data is rendered back to the page, user can evaluate it which is another operation call that can grab some additional data. When the user clicks on evaluate/search or create button, how do I figure out which button was clicked?
It's best practise (because its more RESTfull) if you split up your action in different action/views!

Setting ViewData item in a Partial to be read in a View

I have a View which I call RenderPartial.
In the Partial usercontrol I set ViewData["IsTextAreaVisible"] = true;
In my View after the call to RenderPartial I check the value of ViewData["IsTextAreaVisible"].
Even though the usercontrol had set it, the View thinks that it is null.
Is this a bug or is there a better approach?
Thanks
That is working as designed.
Each partial view gets it own copy of the view data so that any changes it makes don't taint the original. What you want to do, we've expressly prohibited.
I think that the RenderPartial method actually makes a new dictionary out of the object you pass it as ViewData. Since the dictionary is different, the original won't contain any new values you've added to it.
It seems to me though, that if you can calculate the value you are setting in the partial, you ought to also be able to calculate it in the parent view. You might want to think about reversing the calculation and perform it in the parent view and add it before calling the partial. You could always check if it is available in the partial and, if not set, recalculate as needed.
Exchange of data between components of a view looks to me like a design fault. View (full or partial) are there just to passively and stupidly display the model state. All checks, calls and set up of values should be done in models/controllers. I would advice you to rethink you architecture. Even if it seems to you nice and suitable right now, there is a likelihood sooner or later you will have to redesign this piece of code.
Expanding on what Brad said, do it client side with JavaScript. Using JQuery you can find out if there are any visible textboxes on the page and init the client as so.
$(document).ready(function() {
if ($("input[#type=text]:visible").length > 0) {
// inject JS file and init tinyMCE.
$.getScript('<%= ResolveUrl("~/Scripts/tinymce/tiny_mce.js")' %>, function() {
// TODO: call tinyMCE's init function here
});
}
});
That will initialize it only if there are inputs that are visible.
See http://docs.jquery.com/Ajax/jQuery.getScript for restrictions on getScript
Edit: Edited to expand it based on Jon's comment. Note that I haven't run this revised edit in a browser so there may be a hiccup or two. Also, this should really be re-tagged with jQuery if you accept this solution.

Resources