Use an MVC Master Page in a WebForm page - asp.net-mvc

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);

Related

Can I use Razor views in Sitecore similar to the way I use xslt-renderings?

I have been using the sitecore module RazorForSitecore in earlier versions of Sitecore. After the introduction of MVC in the latest version of Sitecore this should no longer be necessary and actually the module is no longer supported. But I am confused about this part of the Sitecore MVC Developer's Reference Guide
Both ASP.NET WebForms and MVC are supported and can be mixed, although a single request must be rendered by either WebForms or MVC.
I am not interested (for now) in using MVC with routing and controllers etc.. I just want to replace my xsl-files with razor-files. One at a time. Over time. But the quote above indicates that this is not possible.
This is however what I could do with RazorForSitecore.
Am I missing something here?
Yes, you can use Razor views in the same way as XSLT, and you don't have to create models or controllers for them.
When they say WebForms and MVC can be mixed, they mean you can have some pages of your site implemented with WebForms and some with MVC, but you can't mix WebForms and MVC on the same page(1). If you want to start moving to MVC you would have to do it to a whole page at a time. So if a page is made from BrowserLayout.aspx, Content.ascx and Widget.ascx you would need to create .cshtml equivalents of each of these.
Note that you can mix MVC and XSLT on the same page so if your site uses mainly XSLTs, it might be that you can start off just converting the layout(s) to cshtml. Once you've done this, you could then convert the XSLTs one at a time.
If you don't care for routing and controllers, you can just create renderings based on the View rendering template. These automatically get the datasource item set for the rendering using #Html.Sitecore().Field("YourFieldName") or if you want to access the datasource item you could use #Html.Sitecore().CurrentItem. This technique is equivalent to using xslt renderings or ordinary sublayouts (except that with ordinary sublayouts, you need to write some code to get the datasource).
(1) unless you're loading content dynamically via ajax, or an iframe

Selected Index Change event of dropdwonlist in MVC4

I am very new to MVC and Presently I need to work on a Project in MVC4 with Razor engine.
So can any one tell me about
How to implement logic for SelectedIndex Change event of dropdwonlist in MVC4?
Thanks.
In MVC you can't do this. You'd need to use jQuery. OnSelectedIndexChange is a concept that is used in ASP.Net, and MVC3 is vastly different. If you want to implement logic when a user changes a DDL, you'd need a jQuery function to run when the DDL changes (onchange html attribute), which would then pass a request with AJAX to a controller on your page. Within this controller you'd be able to implement this logic.
I'd recommend getting familar with the concepts of MVC first. Practises in ASP.Net are rarely interchangeable with MVC.

Is it possible to have a ASP.NET MVC MasterPage that has webforms User Controls which cause PostBacks?

I'm pretty sure the answer is going to be no here, but I just want to be sure. As you can probably guess I'm in progress of converting a web forms project over to ASP.NET MVC. Thus, I have a web forms master page and a MVC master page. The user controls (.ascx) render fine in the MVC Master page, but the post-backs essentially do nothing.
The best solution I know of is to have partials instead of user controls for the MVC master page. And then have an abstract controller that any controller that uses the MVC master page inherits from. Not exactly DRY, but it's the only thing I know of at this point. Any better ideas?
Thanks!
Darren
Controls to postback such as submit buttons would work fine; anything that renders __doPostBack would not work, such as <button>, <a> (or LinkButton), <input type="button" />, etc.
Also, not sure of all the context, but consider the Html.Action MVC 2 syntax (if using MVC 2) as a way to achieve it too, which may be better depending on what's going on within the control.
You're correct - postback from a user control just won't work in a Mvc environment.
What I have done in a similar project is to gradually migrate the UserControls over to Partials called from MVC or from within webforms. [shameless plug] I have written about how to call RenderPartial and RenderAction here: http://www.blog.clicktricity.com [/shameless plug]

ASP.NET MVC dynamically load user control, like in ASP.NET Web Forms

Hallo,
my team and I are about to start a project, and we would really like to use ASP.NET MVC, not the Web Forms. Why? Nothing special, just we want to learn it well and to measure what would be better to use in future projects.
But, our project has important request. We need ability to dynamically load user controls, that can be created and uploaded in runtime. With ASP.NET Web Forms and it's LoadControl method it's simple.
Can we do something like that with MVC?
Tnx
Partial views are what you want.
MVC - Dynamically loading Partial Views
partial views will do the trick.. or you might want to rethink why you are using a user control. is this a third party control? could this be rewritten?
You can use
<%
foreach (var thing in things)
Html.RenderPartial("location/page.ascx",thing);
%>
from within your view

Does the concept of a Control make sense in MVC?

I've started using MVC reccently, and one thing that occurs to me is whether its possible for the concept of a Control to exist in MVC?
From what I see the framework allows the application to be nicely factored into Models, Views and Controllers, but I can't think of a nice way to take a "vertical slice" of that application and reuse it in another application.
What I mean by that is it strikes me that any UI components I build with MVC maybe not very amenable to reuse, in the same way you can reuse a Contol in ASP.NET WebForms. Ok, there are HTML Helpers but I am thinking of something more modular. Something more like the control model in WPF.
Does this dichotomy go to the heart of MVC vs WebForms, or can reusable UI components work in an MVC world?
You can still use ascx files and other features in ASP.NET Web Forms. The only missing piece in MVC is the postback model and view state oriented state management. There is no <form runat="server"> anymore and no automatically generated hidden fields. Except these, all other features can be used in ASP.NET MVC. You can still write controls that post data using a REST based mechanism and use them in your views.
So, yes, as long as your controls don't rely on a server side form for postback, you can use them exactly the same way you'd use in ASP.NET Web Forms.
I was looking for the same thing - for reusable widgets with their own data paths and found this on Steve Sanderson's Blog:
http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/
From the article:
"You’ve heard of partial views, so how about partial requests? Within any MVC request, you can set up a collection of internal partial requests, each of which can set up its own internal partial requests and so on. Each partial request renders a plain old action method in any of your plain regular controllers, and each can produce an independent widget."
This article respectfully offers a alternative to The MVC Contrib Group's Sub Controller strategy (http://www.mvccontrib.org/) which is also a solution for what you are looking for.
I guess its harder in MVC to completely encapsulate rendering and post back handling in a single control you just drop on a page. It some ways this is because ASP.NET Webforms is very abstracted from HTTP semantics and that abstraction is a framework where its possible to create reusable user controls.
ASP.NET MVC doesn't have the abstraction of webforms so you can have a control the posts to three different controllers. While it may feel like your losing easy to use controls when you move to ASP.NET MVC, I think you have a better framework for separating and reusing domain logic.
In ASP.NET MVC you have partial views which can be reused. Rob Conery has a good post on this: ASP.NET MVC: Using UserControls Usefully
I think the closest thing you'll get to an old school controls are partial views, these are effectively just shared markup that you can drop on any page. However they don't have their own controllers out of the box so the code to power the shared UI component (the partial view) would need to exist in the controller of every page that used it. There are ways to reduce the duplication of code but without implementing partial view controllers and binding I don't think there is a way around it completely.

Resources