Ajax in Asp.net mvc - asp.net-mvc

I am using ajax in mvc for partial page refresh.
I create a partial view (ie a seperate user control) for each page and then call partial view by using Html.RenderPartial().
I just want to confirm that do I need to create a seperate user control for every page or is there any other way?
Any suggestions would be greatly appreciated.

You should create a separate user control for each logical unit that you want to partially render. For more fine grained control, I will use javascript (jquery) to manipulate the DOM directly rather than rendering partials.

Related

MVC partial views, partial model, partial controller?

Currently i am investigating if MVC is the way to go for the new major version of our web application. We have an existing web application with webparts, dynamically rendered according to some user settings. Each webpart has its own data and own logic (for example, one webpart with user information, one webpart with currently logged-in users, one webpart with appointments etc. etc.).
What we need to accomplish (i think) is to render a single view, which contains several partial views. Each partial view represents a different model, and has its own logic.
I figured out how to put multiple partial views within a single view, but i don't know how to handle the business logic for each view (in "partial controllers"? if possible at al?), and handle the model for each view?
So the main purpose is to render a page with multiple dynamic views (according to what the user has configured), each with its own logic and data. And then when, for example, a button is clicked in a partial view, the corresponding controller is called to handle the event, and returns the updated partial view. The partial views need to be loosely coupled, and updated async.
From what i've seen so far the most tutorials and documentation are focussing on MVC in general, not on how to separete the business logic and model for eachr partial view.
So I'm not asking how to do this, but:
Is it possible to easy accomplish this with MVC 4 or 5?
Does anybody know a good real-life example or tutorial about this?
I hope anyone can point me in the right direction or share some thoughts on this...
You could make one or more controllers with an action for each webpart.
Group all related webparts in the same controller but make an action and View+ViewModel for each webpart. Then use the Html.RenderAction method to call these actions (and have your webparts placed) on your page/main view.
DISCLAIMER: This said, each call to Html.RenderAction creates a complete mvc flow, instanciating a controller, model and view and finally renders the whole thing and passes the value to your page/main view. Having lots of Html.RenderAction has the potential to slow your page creation a lot. You could look into DI/IoC like Unity and consider reusing controllers, or just look into System.Web.Mvc.DependencyResolver to handle the creation of controllers yourself.

ASP.NET MVC Custom Controls

Is it safe to say that custom controls with ASP.NET MVC are most times just partial views? And if that's the case, I'm guessing it's always up to the implementing application to dictate the behavior (through controller code) of these controls?
I have done a bit of searching, and there is almost no resources on ASP.NET MVC custom controls (either that, or I'm missing the mark with my Google skills).
Partial views are more a template for either a control or a set of controls that can be shared between views.
I'm not sure they are "Custom Controls" as such and it sounds like you are coming from an ASP viewpoint.
I think you need to first get in the mindset of MVC and out of ASP.
So for a list of items you may have a partial view that takes the list of items and a partial view that takes an actual item. So "pvCustomerList<List<customer>>" and "pvCustomer<customer>".
The pcCustomerList iterates through the list and creates a pvCustomer for each customer in the list.
Partial Views don't really have code in the controller. Instead they are passed data from the view. If there is a submit action in the partial view, then this is either handled by the controller for the view or a jQuery post back.
I hope this clears things up a little for you.
Have you checked out NerdDinner sample?
There is nice article on asp.net mvc website regarding form helpers (helper methods for views). there is explained how to create custom helper method
Link is http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

ASP.NET MVC structure and how to use the Actions and Controllers

I am developing an application in ASP.NET MVC structure.
I was wondering, I am trying to create a site that only has one page, it has a navigation bar on the left (Which is a list of user input), and then the main content in the middle.
My question is, can I make the navigation bar (The user input list) static, and make the main content change, based on the user input, with an asynchronous AJAX postback? I would have multiple controllers which would set different results to the main content, based on the user input.
How would I go around doing so, partial views and calling actions with asyncpostback's?
Yes, I would do exactly as you have suggested - have actions that render PartialView([name]) and use, probably, jQuery's ajax to fetch the content.
You can then use jQuery to inject the html into the content panel.
Where it might get interesting is if you need to have css added to the head section of the page.
But then, with diligent use of a single CSS, or at least a single group of CSS files, you can sidestep that.
Be aware though, that it does complicate form post-backs if the dynamically injected content has forms. You would need to hijack the form submit process and turn it into another ajax call to get the resulting html into your content panel - you might even need to hand-crank the form submission because I don't think you can trigger form submission as an Ajax operation.
I've done a similar thing with named iframes (doesn't necessarily need script), as you can render a form with a target which is equal to the name of an IFrame; and it means that the iframe content can be a full view instead of a partial (and therefore has full control over its own script and css). Of course, then you have issues with sharing data between the host page and that frame.

Use an MVC Master Page in a WebForm page

I have an MVC application that I am creating that I have to integrate regular WebForms into the site. The WebForm pages will be using a ReportViewer control so I believe that won't work in a regular MVC view because ReportViewer uses ViewState, etc.
Is it possible for me to create a regular Web Form that uses an MVC View Master page? If so...is it possible to use the Html helper methods such as RenderPartial?
Is it possible for me to create a
regular Web Form that uses an MVC View
Master page?
I don't know for sure, but it might work, as long as it's just markup and doesn't use any MVC-specific features.
If so...is it possible to use the Html
helper methods such as RenderPartial?
Nope. If you use it this way, the Html property will not be set automatically, and I don't know of any way to hack it in.
We began a project in WebForms and realized partway through that MVC suits our purposes far better, so until we can finish migrating away from WebForms we have to maintain two separate versions of each of our "portal" elements (tabs, logout buttons, etc.). It's a pain, but it's doable.
Instead of using RenderPartial you could call a MVC controller with ajax from your webforms app and use the html it returns.
if (Request.IsAjaxRequest())
return View("someascx", model);

can i open views in an ajax modal popup?

I've been searching about using ajax modal popups in asp.net mvc, and i've only seen uses where there's a simple dialog box for input.
Is it possible to open an entire view in an ajax popup? Is it possible to navigate multiple views in a single modal popup instance, as if they were going through a wizard?
I haven't found any good references about this, I would appreciate any and all links to relevant info! thank you !!
You can load any response type your heart desires via ajax and in a modal popup. Most of the good major modal plugins (jquery ui, colorbox, jquery toolbox, etc) have some simple events you can plug into. If the modal plugin doesn't natively support it you can do a simple call to JQuery's "load" method.
$("#modal-dialog").load("/ajax/url");
Create a simple route for "ajax/url" and you're set. You're probably better off returning a partial view for that "ajax/url" action but you can also return plain text, or whole page if you want.
Moral of the story is to set up an action that returns what you need (text, html, xml, etc). Make sure it can be accessed with a route, and use jQuery's ajax methods to fetch it.
using jQuery UI's dialog plugin, you could do something like:
$("#id").load(url).dialog();
and 'url' would be an action on your controller that returns a partial view. then you could put links in your partials that trigger a .load() with the url of the next partial in the wizard.
jqModal also has an AJAX loading feature:
http://dev.iceburg.net/jquery/jqModal/#examples

Resources