Rendering Views using ASP.NET Webforms and Model View Presenter (MVP) Pattern and AJAX

by jmorris 1. May 2009 21:49

ASP.NET MVC introduces the notion of partial views, which either allow specific Actions to be called on a Controller and the resulting view outputted as HTML or allow Model Data to be passed from the current view to another, child view.

Calling Actions on a Controller and returning “sub” Views is incredible useful in situations were Ajax type behavior is required, since the resulting output is simply raw HTML. The HTML can then be appended to a DOM element via JQuery, some other JavaScript library or simply with straight JavaScript.

Unfortunately if you are still working on Web Forms projects or you have no need or desire to drink the kool-aid of MVC, you are pretty much out of luck for similar functionality built into Web Forms. However, it is fairly easy to implement this sort of behavior within the context of a Web Forms environment and then use a Ajax to render pure HTML from these “partial views.”

The code for doing this is surprising simple and was adapted from a Scott Guthrie posts a few years back:


In this case I allow a Dictionary containing the name value pairs of the properties on the View that will be loaded to be passed in as well. The key must match the name of a Property on the View and if it does the Property is set with the value of the key. This is useful in cases were you would like a parent View to pass data to a child View for initialization situations, etc.

I also have a static collection of Views defined which map a View’s name to a path in my applications root directory. RenderView looks up its path and loads the control before setting the Views data. In my case I load this from a table via a Data Access Object (DAO) in the Global.asax on Application_Start:



The PageUserControlDAO loads the View definitions from the database once when the application is started or the App Pool is restarted/refreshed.

Rendering the View involves calling the RenderView method and passing in the appropriate View name and a Dictionary containing the data to into the view:



In this case I used Page_Load to render the View into a panel (div) which isn’t too exciting. The really useful aspect however is when you combine JQuery and Web Services to render the View on demand from the client.

Rendering Views from the Client Using JQuery and Web Services

The Web Service:



The JQuery code to load the view and attach it to the DOM:


In the JavaScript above, renderView takes three parameters: the name of the View to render, the data to pass to the View, and the DOM container to load the view into. The $.ajax(..) method is a JQuery method that specifies the WebService to use, the method to call (RenderView) and the content type to specify in the request header.

When the method is called like so from the client:

 

renderView('ImageView', { 'CurrentPageElementId': 100 }, $view);


The WebService method is invoked, which renders the view and returns pure HTML that is then appended to the DOM element $view (a simple div or span).

Tags: , , , , , ,

Comments

4/22/2011 2:46:11 PM #

JD Edwards EnterpriseOne

The problem is that the HTML coding can be appended to a DOM element via JQuery, some other JavaScript library or simply with straight JavaScript.

JD Edwards EnterpriseOne South Africa | Reply

8/4/2011 7:05:21 PM #

Lars Elrund

That's great, but I still wonder what will happen when the JavaScript will become more complex? This is usually the case with Web2.0 pages that have many ajax calls, a lot of logic behind them and they modify the DOM extensively.

I would put more wieght on the JS/jQuery code: larselrund.com/2011/08/03/mvp-with-javascript/

Lars Elrund United Kingdom | Reply

11/23/2011 12:02:20 PM #

Technology Blog

Just want to say your article is as astounding. The clarity in your post is simply spectacular and i can assume you are an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please keep up the enjoyable work.  Thanks !

Technology Blog | Reply

11/25/2011 11:21:35 AM #

t mobile contract

Beside that I means T-mobile, three, 02 and Vodafone UK based mobile phone shopping has many sides which impressed me so much.

t mobile contract United Kingdom | Reply

11/25/2011 1:06:30 PM #

Toronto condos

I have viewed that clever real estate agents all over the place are starting to warm up to FSBO Marketing. They are noticing that it\'s more than just placing a sign post in the front property. It\'s really in relation to building connections with these vendors who sooner or later will become purchasers. So, after you give your time and efforts to helping these sellers go it alone -- the \"Law regarding Reciprocity\" kicks in. Great blog post.

Toronto condos | Reply

12/6/2011 2:46:28 PM #

Levrige Business Evolution Consultants

As a business owner you have many challenges and responsibilities.  Using Internet technology correctly can help you grow your business and increase sales quickly and easily without a huge investment.  Evolve your business and start working less while making more by leveraging Internet technology in your marketing.

Levrige Business Evolution Consultants | Reply

12/10/2011 11:22:09 AM #

web hosting reviews

Thanks  for the awesome share.
Lots of blogs like yours cover subjects that aren’t found in magazines

web hosting reviews | Reply

12/10/2011 3:36:50 PM #

CraigsList BackPage EZ-ADs

Give any thought to advertising your business on CraigsList or BackPage, we can help for pennies on the dollar, for little of nothing:

CRAIGSLIST ADS POSTING - http://EzAdBlaster.com
BACKPAGE & CRAIGSLIST Ad Posting Service - Simple - The More Ads Posted, The More Sales - Simple !
.001 Per Ad Posted Penny Promotion, Take Advantage of It NOW !
BACKPAGE & CRAIGSLIST Ad Posting Service -Sales Is A Numbers Game -Simple-More Ads Posted=More Sales
http://EzAdBlaster.com

CraigsList BackPage EZ-ADs | Reply

12/20/2011 1:02:12 PM #

M Nicklaus

Hi , I really hope the property market is a great deal more suitable in 2012. I hope you as well as your loved ones have a well deserved as well as Merry Christmas and I hope 2012 to be a fantastic year for you.

M Nicklaus | Reply

12/21/2011 6:29:25 PM #

rdawson

Thanks for ones marvelous posting! I really enjoyed reading it, you\'re a great author.I will always bookmark your blog and will often come back at some point. I want to encourage you continue your great job, have a nice weekend!
We absolutely love your blog and find nearly all of your post\'s to be exactly I\'m looking for. Does one offer guest writers to write content to suit your needs? I wouldn\'t mind producing a post or elaborating on many of the subjects you write with regards to here. Again, awesome weblog!
My spouse and  I stumbled over here coming from a different page and thought I might check things out. I like what I see so now i am following you. Look forward to looking into your web page for a second time.
I really like what you guys are usually up too. This sort of clever work and exposure! Keep up the terrific works guys I\'ve incorporated

rdawson | Reply

12/23/2011 5:31:46 PM #

downtown toronto condos

Hey , I’ve just started my own online presence for my business, but there are some great ideas in the list that I really could do and hadn’t thought of at all. I would like to add this to my real estate FB , not sure how to do it yet, but I will figure it out somehow.

downtown toronto condos | Reply

12/23/2011 11:00:15 PM #

Toronto condos

Hey , Good point !
I’ve just started my own online presence for my business, but there are some great ideas in the list that I really could do and hadn’t thought of at all. I would like to add this to my real estate facebook , not sure how to do it yet, but I will figure it out somehow.

Toronto condos | Reply

12/24/2011 2:04:52 PM #

apartments toronto

I have been browsing on-line greater than three hours lately, but I by no means discovered any attention-grabbing article like yours. It is pretty worth enough for me. In my view, if all website owners and bloggers made excellent content as you probably did, the web will likely be a lot more useful than ever before.

apartments toronto | Reply

1/2/2012 2:01:38 AM #

Dave Summers

Very good blog post.  I always mention that one of the most important reasons for first aid is to preserve Life, not only the casualty\'s life, but your own as well. Far too often only one person\'s life
is in danger when the emergency services are called, but by the time they arrive there
are more. If you put your life in danger, you can end up fighting for your OWN life
instead of the casualty\'s.

Dave Summers | Reply

1/3/2012 5:04:24 AM #

Rob

Hey  social media has come a long way from being a mere outlet for sharing thoughts and images among friends and families. It has become a popular and effective method for affiliate marketers…

Rob | Reply

1/8/2012 11:15:36 PM #

Good Deal

You should take part in a contest for one of the best blogs on the web. I will recommend this site!

Good Deal | Reply

1/12/2012 7:19:03 AM #

Jenn

I Just Wanna Say,

I just wanna start out by saying everyone is different. If you follow a good plan and really stick with it you will see great results. Good Luck Everyone

Jenn | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Who Am I?

My name is Jeff Morris a software engineer/developer in Southern California, USA. My platform du jour is dotnet, but I dabble on the darkside occasionally.

Tag cloud

Page List