Embedding an MVC form inside the old master template architecture - asp.net-mvc

I have an old Umbraco web application which was upgraded to v6 earlier this year.
It uses .NET ascx user controls and your standard .master pages as you'd expect.
I now need to create a brand new page which is essentially a booking form for an event.
I'd like to create it using MVC, as I do everything with MVC nowadays. I don't really want to duplicate the page layout / the frame. Is it possible to embed an MVC partial view inside the old master templates?

You can create the form in a .cshtml file and then add it as an Umbraco Macro. But the form couldn't use the MVC approach, it would have to be basic inline razor plus conditional statements. Which is fine but limiting. You could of course have the form posting to a controller action too but this get's a little tricky as it takes the form processing out of the page's event cycle.
Otherwise you are asking if you can reference an action in a masterpage, so no not really. I say "not really" because of course you can but it's unnatural and would make the project a nightmare for anyone to undestand of they were to pick it up. There is an article here as to how you might do it tho': http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
Also, there is also the Umbraco MvcBridge package. This was something a lot of devs used prior to being able to build directly in MVC. Essentially you can set up Macros to point to actions etc. but still within the webforms mode.
It's a great half-way house but I'm not sure abut the compatibility with v6.

Related

ReportViewer in MVC 4 partial

I am still unsure the best way to go about it.
I've read alsorts of resources, yet still no closer to a working solution.
I created a partial ASCX file. Added Report viewer to it, then rendered said partial in my CSHTML file. This was the closest I have come. In that I can see the report viewer, buttons etc. But for some reason the data never shows.
I have tried ASPX page inside an IFrame But this is not the way I want to go, about making this solution work.
Loading an ASPX page outright. But I lose my _Layout.cshtml main page style.
Some people suggest changing all sorts of things in config / Global.asax where as some say its not even necessary.
Ideally I want to have it as a partial in a page. But can deal with it being the only thing on the page, if I keep my main layout.
My experience with the older syntax / pages / non-MVC is limited to this project - trying to get this to work.
My data is linked through the components setup. The connection works fine in aspx page, as single page or iframe. But not in ascx.
The ReportView control requires ViewState to be enabled. In ASP.NET MVC such notion no longer exists. This means that you cannot use this control inside a native ASP.NET MVC view or partial. You can use it only in a classic WebForm (not in an ASP.NET MVC View) and the embed this WebForm inside an iframe within your current ASP.NET MVC view or partial. You have the possibility to render the report as an image for example and directly stream it to the response without using this control. Here's a blog post which illustrates the technique. So you could have a controller action which generates the report as a JPEG image and then link to this controller action from your view using the <img> tag. Unfortunately this makes only a static copy of the report and the user cannot interact with it.
You might also checkout this blog post which illustrates how you could run ASP.NET MVC and classic WebForms side by side.

Can MVC framework be used in a web application that is currently using ASP .NET?

I recently joined a group that manages a Classic ASP web application. It has been working fine for our group's need. However, a decision was made, before I joined, to move to ASP .Net. Since we are mostly ASP developers, we write code in ASP .Net as we would in Classic ASP (for the most part). Would it be possible to introduce MVC to this application/project?
Thanks!
Yes, it is possible to use MVC in a traditional WebForms project. I migrated a large WebForms project to MVC 2 a couple of years ago, and here are my findings (I have updated them to reflect MVC 3)
Make sure you have .NET 4.0 installed, as well as the MVC 3 framework and VS extensions.
Create a new blank MVC project to use as a reference.
Look at the default web.config for the reference project. You basically want to use the reference web.config, and merge in stuff you need from your current project.
Look at the reference global.asax.cs. Similar to the above, you want to merge the changes in the reference .cs into your current application's global.asax.cs.
You will need to add the following references to your web project:
System.Web.Abstractions,
System.Web.Extensions,
System.Web.Helpers,
System.Web.Mvc,
System.Web.Routing
You can enable the VS extensions by changing the ProjectTypeGuids:
In Solution Explorer, right-click the project name and select Unload Project. Then right-click the project name again and select Edit ProjectName.csproj.
Locate the ProjectTypeGuids element and add {E53F8FEA-EAE0-44A6-8774-FFD645390401}.
Save the changes, right-click the project, and then select Reload Project.
Add the following standard folders for MVC content:
~/Views
~/Views/Shared
~/Controllers
~/Models (for your view models, optional)
~/Content (for CSS and images, optional)
~/Scripts (for JS, optional)
Additional notes:
If your existing WebForms relies on web.config settings for authorization (such as preventing unauthorized users), this won't be recognized by MVC actions, because routing works completely separately from the WebForms authorization. Use AuthorizeAttribute to require authorization, or constrain actions to certain roles or users. You can even specify global filters so you don't have to apply this attribute on every single controller or action.
There may be additional considerations for making MVC work with IIS versions prior to 7, or with application pools that use the Classic pipeline. Consider using IIS 7+ with Integrated pipeline.
My notes above mainly involve getting the baseline of MVC working, which uses ASPX views. ASPX views use the same markup as the ASPX files you are used to in WebForms. You can also use the new Razor syntax (primer), which I highly recommend. You can use both ASPX and Razor view pages at the same time. However, you cannot use an ASPX master page on a Razor view (or vice versa). Also, MVC will find and use ASPX views before Razor views, so if you upgrade a view to Razor, delete the original ASPX. You will need to do a little additional work to enable Razor views. I'm trying to find my notes for enabling Razor. I'll update when I find them. Once you do have Razor installed and working, you can use this tool by Telerik to convert ASPX to Razor.
Here is a question on SO about a problem I'd had while upgrading. I'm only providing it because it covered some of the points I mentioned above in more detail. However, I was upgrading to MVC 2 at the time, so some of this stuff is out of date.
The short answer is yes it is possible.
Scott Hanselman has written about this topic before.
Depending on the skills in your team, you may well find it difficult to get up to speed - it is possible to write clean MVC style code in Classic ASP but most people don't.
It is of course technically possible. However, it sounds like it would be a cultural shock, as MVC works quite a bit differently than Classic ASP or ASP.NET WebForms. I think it would be worthwhile to do - or just stick with ASP.NET WebForms if that seems more natural. But if you've not yet fully committed to WebForms, MVC seems just as easy to move to, IMO.
Yes. I worked on a project that began as plain ASP.NET and later added some ASP.NET MVC pages. Eventually we liked MVC so much more that we eventually migrated all of our WebForms pages to use MVC instead. But the whole time, the two systems worked very well together.
Here's the only real gotcha that I can remember running into: WebForms works by having the entire page encased in a big <form> tag. Since HTML doesn't allow you to have nested <form> tags, you typically can't use MVC forms inside of a WebForms page. Either keep your WebForms pages separate from your MVC content, avoid using HTML forms in MVC content that may appear on a WebForms page, or use popup dialogs for your forms that get created outside of the WebForms DOM area.

Design Surface in ASP.Net MVC

Are there any recommendations for a design surface (or design tool) that could be used by a designer (the dude/dudette with the black turtleneck) in the process of building an ASP.Net MVC application?
Such that once there is agreement on the structure of the Model, and the interactions required by the app, then the designer goes away and builds out the UI (V in MVC) using this design tool, while the developer goes away and builds out the code (M&C in MVC) using Visual Studio.
As I understand it now, this designer person would also need to use Visual Studio and build the Views using Razor (or other view engine) syntax, instead of having the ability to build the Views using a design surface with drag-drop layout and property settings and the like.
I think you could ask them to give you a harcoded HTML for each view... then, you replace the harcoded data once you place it in Razor.
That is the beauty of Razor... it is very easy to pass from fixed HTML to a razor view.
Razor requires programming, and creating views is not an arbitrary task. I'd let the designer work in whatever program they like where they can excel about design and let the programmers worry about how best to create the views and programming in Razor (which is really just C#/VB with some extra syntax).
It depends.. what kind of skills does your designer have? Are they a true designer (no JS/jQuery/etc knowledge.. they live & breath PSDs) or are they a designer/front end engineer (they know js/jQuery, the diff. between IE7 and IE9 from a rendering pov).
If you've got a true designer then the best thing to get would be HTML. A PSD would be okay as well but if they can convert their design into HTML they've done a lot of the hard work for you (unless you love figuring out floats and margins and all that jazz). That said if you're using something like Twitter Bootstrap or Blueprint CSS then they would obviously have to know how to use that as well.
If you're lucky enough to have a designer/front end engineer then it's well worth your time to teach them some basic razor synax like #Url.Action and #Html.BeginForm. They can tell you the actions they need and you can work together by giving them a fake data/response version first which they can use while you create a real version. And in this case they can either use Visual Studio OR you can set it up so that they have your site hosted via IIS on their machine and they just use your source control to get latest which automatically gets placed in the right directory. Then they can just continue working in whatever editor they prefer as they should only be working in html which will get updated live. That said if you're using ASP.Net MVC 4 bundling you'll have to decide how to maintain bundles.

Upgrading ASP.NET MVC2 project to MVC3 and using mixed View Engines

Can I upgrade my MVC2 project to MVC3 and continue to use my ASPX views, AND at the same time start writing some new views in Razor?
More specifically, Can I take an ASPX view page, and a Razor control in it? Would such a thing be possible?
Any other things one should look out for?
You can definitely mix view engines across totally separate views. As for putting Razor "controls" in an ASPX page, if you use RenderPartial, it should work.
One thing I would watch out for is that if you use Master Pages then you may end up duplicating them for both view engines. I had a good ol' WebForms Master Page, used by all my regular .aspx content files, but creating a new view using Razor means I have to use a duplicate Razor layout page alongside that old Master Page.
I haven't looked into it too much, but at first try I get 'The file "~/Views/Shared/Site.Master" could not be rendered, because it does not exist or is not a valid page.' and certainly there's nothing in Scott Gu's blog entry introducing Razor to suggest you can combine them, but I could be all wrong here.
http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
For how to mix view engine master pages...

ASP.NET MVC RC - Creating a MVC User Control with a codebehind

Trying to create a MVC User Control in the Release Candidate and I can't see to make one with a codebehind file. The same is true for MVC View pages.
Creating Views in the Beta would produce codebehinds...am I missing something?
Code behind kind of defeats the purpose of the MVC Framework. Functionality should be kept separate from the view, the MVC team felt that code behind pages went against this ideology and therefore removed them.
Your can create a custom helper method to create your control. Also I'm not sure if MVC has view components (Monorail/Castle) but that could be an option as well.
From ScottGu's Blog post:
*Views without Code-Behind Files
Based on feedback we’ve changed view-templates to not have a code-behind file by default. This change helps reinforce the purpose of views in a MVC application (which are intended to be purely about rendering and to not contain any non-rendering related code), and for most people eliminates unused files in the project.
The RC build now adds C# and VB syntax support for inheriting view templates from base classes that use generics. For example, below we are using this with the Edit.aspx view template – whose “inherits” attribute derives from the ViewPage type:
One nice benefit of not using a code-behind file is that you'll now get immediate intellisense within view template files when you add them to the project. With previous builds you had to do a build/compile immediately after creating a view in order to get code intellisense within it. The RC makes the workflow of adding and immediately editing a view compile-free and much more seamless.
Important: If you are upgrading a ASP.NET MVC project that was created with an earlier build make sure to follow the steps in the release notes – the web.config file under the \Views directory needs to be updated with some settings in order for the above generics based syntax to work.*
I answered this question here:
How to add a Code-behind page to a Partial View
Seems this wasn't particularly tricky, and is quite do-able
This answer worked for a Partial 'ViewUserControl' but the same should apply
Ok.
First: Add a Class file with the convention of .cs (i.e. view.ascx.cs)
Second: Add "using System.Web.Mvc;" to the class
Third: Change the Class to Inherit from "ViewUserControl<>"
Fourth: Add the following to the View's header:
CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"
Fifthly: Copy the files out of the solution and drag back in to reassociate the two together
Note: For this to work with a Normal MVC View you just need to inherit the class from "ViewPage"
The whole idea for ASP.Net-mvc was to get rid of the codebehind files...thats why asp web controls didnt matter that most didn't work.But with the changes of getting rid of the code behind comes with a different programming style..The idea is codebehind files are EVIL:
http://stevesmithblog.com/blog/codebehind-files-in-asp-net-mvc-are-evil/
the whole idea is to make sure people remember they are using asp.Net-mvc and not asp.et web pages. take alook at this link ,it explains it a little better:
http://blog.lozanotek.com/archive/2008/10/20/Visual_Studio_Templates_for_MVC_Views_without_Codebehind_Files.aspx
I think this tutorial is what you are asking.. but not really sure what you want..

Resources