Asp.net mvc ajax site.master - asp.net-mvc

I want to use a different model for site.master. Instead of of the old "<"asp :Content PlaceHolder ID="SideContent" runat="server" "/ >" tags I want to use ajax to dynamically display my aspx pages(within Site.master). so no complete reload unless explicitly asked for.
I also want to do this with very little change to the way my project is organized.

Joe,
if you're using mvc, it'll be no problem (jQuery partialview updates etc, etc). however, as you haven't tagged as such i'm presuming webforms (oh, i see the asp.net mvc tag is there now - however, i'll leave the reply as i wrote it at the time as it may be of interest). the good news is, all is not lost and there are in fact page methods that can be used in webforms to stunning effect. i did a quick google and this article seems to nail what i was thinking about:
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
i can't comment on the technique too much as i'm definately more in the mvc camp these days, but this is how i'd have done it had i been still heavily using webforms.
jim
[edit] - as the mvc tag is now present, that changes my answer above (or at least means that i should add a little re mvc). what you could do on the masterpage is to ensure that your content pages were ALL partialviews and that your menus invoked a partial refresh. the masterpage would have a 'content' div that you'd update via each jquery menu request. in effect only the inner section of the page would ever change. i know you'd said 'little' change, but depending on the width of your pages, this suggestion could be fairly simple or complex depending on your viewmodels etc. i'll be interested to see further comments.
also, take a look at this (oldish) jeff palermo article re partials and different masterpages: http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/

Related

Asp.NET MVC view engines, why they don't support "View designer" now?

With the asp.net MVC, we see a lot different view engines, like Razor, spark, webform etc.
I thought the idea of MVC is separating of data and view, I assume view part should be something that allows a designer to do some work, even after it is created by a developer. But I see now most if not all view engines introduce new syntax, not stick with html. The old web form, you can use "view designer" to see how a page looks like even with some code blocks, designer could at least move blocks and html elements around, but now with engine like Razor, you cannot even view a page in designer mode. So I don't quite get what is the point?
This came to me when I tried to search for a server side templating that allows end user do some changes on the page. I perfer something stick to pure HTML, maybe Spark is the one, but I am not sure. Please someone can give me some idea.
Thanks
Views are meant for that. It will not static design. Markup will be generated on fly.
If you take razor as an example, you will have the combination of c# and html as code in view page. So, as you may be able to understand now, you cannot see the actual design until the c# and html execution goes hand-in-hand.
Hope it clarifies a little!

Old webform page in MVC masterpage for quick conversion

I was wondering if anyone had any experience using a webform page with a MVC MasterPage or even better a Razor _Layout page.
The company wants to convert to MVC but they don't want to feel any pain and they want to be able to retro fit their old pages very easily. After the typical grumbling about investment in infrastructure and extensibility I realize that I need to get them on the platform and then work them into the modern era slowly. It's not optimal but it's a foot in the door.
So what I'm hoping is that I can build there masterpage ( header, menu, footer ) into MVC ( actually already have ) and then remove the masterpage from their webform pages and load them in the MVC layout or masterpage.
I figure I can do that with ajax, but I would really like to not have to write wiring for this to happen as a) they will see this as overhead and hacky ( and I agree ) and b) there are a ton of old pages and I don't want to be at it forever.
Any thoughts or experiences would be appreciated,
R
you can try use HttpHandler or HttpModule and at the end of request put section via replace("[SECTION_NAME]", getSection). In MVC you can geberate html from vew and model, but for this you need execute request to server. So i think it will be bette if you cached this part... some like that.
OK, so this was a fool's errand, clearly. The premise was sound, get their regular pages into an mvc masterpage then you have sufficient buy in to move forward. But it's a colossal pain in the ass and possibly not even doable. Perhaps in the future when they have assimilated WebForms in MVC, which is their plan although I can't see how it's possible or even desirable, but not now.
I got some of the way courtesy of this post. It actually loaded up the page in the MVC ( had to be webforms, not razor ) MasterPage. But when the WebForm did it's ridiculous "post back" pseudo-event thing it called the controller, rather then the page. At which point I threw up my hands and decided to go the political route rather then the back door route.
Like I said in the future this maybe possible, and if you really want to dig it may be possible today, but as the cutting and rather snide comments point out it's kind of a dumb idea to begin with.
R

Do a full non-ajax post from a partial view in asp.net mvc

This more of an out of interest question than an urgently need an answer one, but I have been trying to find a good example of how to deal with a full postback from a partial view in asp.net MVC. The obvious example is the case where you have a small login form on every page. You can easily accomplish this through an asynchronous post back using jquery, but I am wondering if there is a way to do it without the use of javascript. I know it may be pedantic, but I don't like the idea of assuming the client has javascript enabled, particularly in this day and age where responsive design/ progressive enhacements are the big buzzwords around, so having you log in tied to javascript means that anyone on a simple mobile device won't be able to use it since their device probably won't support it.
Anyone have any ideas / examples of how to accomplish this? It's such a simple thing to implement in web forms I can't believe it's as tricky as I've heard it made out to be in MVC.
You just need a form within the view, that's all. The form will POST to its controller action method and generate a full page refresh (if that's what you mean by a full postback - I guess it is) irrespective of whether its a partial or not.
You can have multiple forms on a MVC view, and each one of them will give you a full page refresh, whereas with WebForms the pattern was one main form per page.

MVC vs ASPX dynamic page render

I have a CMS website written in aspx 2.0 that allows users to build up pages by dropping controls on a page and setting properties (reflection on the server side) and events (client side js). The render engine knows what property to call on each control to find out what to save into the database. I went through all the pitfalls of control re-hydration and lack of proper ids on the controls and really struggled to make the solution SEO friendly which currently is partial at best. Also suffer from viewstate getting huge so I have started to look at MVC as a better way forwards for the next version. The final design of the page is only set when the user decides to promote it live, and may make many changes per day.
A typical page may have many textbox controls, radio button groups, checkbox groups, dropdownlists and images. Also we have a few of our own controls that we reflect at runtime into the solution.
From my initial research into MVC, this looks to have been written to avoid these types of issues and not try to hide the html which looks very promising as well as giving final markup that is much more cross browser friendly.
Now the question - since the requirements are to generate dynamic pages with dynamic html controls, is this a step too far for MVC and I should stick with ASPX, or is there a way to generate the dynamic content which would be suitable for a non technical person to be able to do?
Really appreciate any guidance before I jump in with both feet :)
Thanks
Mark
I'm assuming by aspx 2.0 you mean WebForms? It's really not a question of if MVC is capable of doing what you need - it is capable, and in
my opinion it's more capable. However There are some major differences between WebForms and MVC check out this post for more on that topic: MVC versus WebForms.
I hope this helps. Without more information on exactly what you're trying to accomplish, there's not much more I can say. Consider asking more specific questions with some code examples.
Some of the main advantages of MVC: Clean HTML, No ViewState written on the page, easier to support html5 and thus SEO as well.
For me, since I used MVC 3 years ago I don't even want to touch WebForms thesedays.
Btw, if you want CMS + MVC, why not use Orchard rather than building yourself?
http://paulmason.biz/?p=118

What's the best way to implement user controls in ASP.NET MVC?

Like many others on this site I am considering a move to ASP.NET MVC for future projects. Currently my sites are running the traditional ASP.NET 2.0 Web Forms, and it works OK for us, so my other option is just to stick with what I know and make the move to ASP.NET 3.5 with the integrated AJAX stuff.
I'm wondering about how user controls work in ASP.NET MVC. We have tons of .ASCX controls, and a few composite controls. When I work with web designers it is very easy to get them to use ASCX controls effectively, even without any programming knowledge, so that's a definite plus. But then of course the downsides are the page life cycle, which can be maddening, and the fact that ASCX controls are hard to share between different projects. Composite controls are share-able, but basically a black box to a designer.
What's the model in ASP.NET MVC? Is there a way to create controls that solves the problems we've dealt with using ASCX and composite controls? Allowing easy access for web designers without having to worry about code being broken is an important consideration.
To implement a user control you do the following call:
<% Html.RenderPartial("~/Views/Shared/MyControl.ascx", {data model object}) %>
You may also see the older syntax which as of PR5 is not valid anymore
<%= Html.RenderUserControl("~/Views/Shared/MyControl.ascx", {data model object}) %>
You will always have to worry about code breaking when moving from Web Forms to MVC, however the ASP.NET MVC team has done a great job to minimize the problems.
As Nick suggested, you will indeed be able to render your user controls, but obviously the page-cycle, pagestate and postback from traditional ASP Webforms won't work anymore, thus making your controls most likely useless.
I think you'll have to rewrite most of your complex controls to port your website to MVC, while simple controls which, for instance, provide only formatting and have no postback status, should simply work.
The code provided by Nick will simply work in this case.
And about sharing between more projects: I think controls will be more like "reusable HTML-rendering components" that can be shared across a website, rather than "reusable code components" with logic (like WebForms controls). Your web logic will/should be in the pages controllers and not in the HTML controls. Therefore sharing controls across more projects won't be so useful as in the WebForms case.
MVC has different page life cycle compare to your user control.
You may consider this to re-write.
The aspx is the view. You still need a re-write, the syntax is different.
JavaScript will work. But I hardly find the WebControls will work. Because MVC does not have viewstate and postback anymore.
For the code behind (aspx.cs) you need to convert that to be a Controller class.
Page_Load method will no longer works. You probable leave it to Index() method.
Model is simply the entity classes that your code behind consume.
Conclusion, it's a total rewrite. Cheers. Happy coding.
Yeah, you can do RenderPartial. That's a good start. But eventually these guys will need logic and other controller type stuff. Be on the lookout for a subcontroller implementation from the framework team. There should also be something in MvcContrib soon. Or roll your own.
Edit: I just posted about this here: http://mhinze.com/subcontrollers-in-aspnet-mvc/

Resources