JamesBrownIsDead
JamesBrownIsDead

Reputation: 57

ASP.NET: Using MVC partial view in web forms

I've got a regular ASP.NET web form page and am pasting in a <%= Html.Partial(...) %> that I took from an MVC view page.

What do I need to do to this web forms .aspx to get the Html.Partial to work? I've put this at the top of my web forms .aspx page:

<%@ Import Namespace="System.Web.Mvc" %>
<%@ Import Namespace="System.Web.Mvc.Html" %>

But I'm still getting error:

Compiler Error Message: CS0103: The name 'Html' does not exist in the current context

Upvotes: 2

Views: 3104

Answers (2)

ten5peed
ten5peed

Reputation: 15900

You are getting the error because Html is a property of the System.Web.Mvc.ViewPage class and is an instance of the HtmlHelper class. The System.Web.Mvc.ViewPage class is the class that your default ASP.NET MVC views inherit from, so you have access to the Html property in your views.

I don't know how you would go about creating an HtmlHelper instance in your webforms page but I would imagine that you'd have a hard time because it's constructor takes a ViewContext and an IViewDataContainer.

Upvotes: 5

Dustin Laine
Dustin Laine

Reputation: 38553

Here is an article about how to use MVC in webforms: http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc

Upvotes: 2

Related Questions