.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

MVC3 Beta Razor Views - not partial classes

Posted By:      Posted Date: October 12, 2010    Points: 0   Category :ASP.Net

Why are Razor views not generated as partial classes? Making them partial classes gives us the option to have a code behind where we can do some of the stuff we're forced to do in the template itself in the code behind file.

Yes, I know the standard "go-to" thing is Html helper but I don't want to use Html helpers for these things as they are too specific to the view in question and besides, having a clutter of extension methods is just not the way to go.

I'd like to see Razor views generated as partial classes.

View Complete Post

More Related Resource Links

ASP.NET MVC3 Areas and Razor views produce errors?


The view at '~/Areas/SomeArea/Views/List/Index.cshtml' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.

The project structure is pretty much default. There is one area called SomeArea. It has a single controller called List. It does nothing except:

public ActionResult Index()
    return View("~/Areas/SomeArea/Views/List/Index.cshtml");

The view looks like:

@inherits System.Web.Mvc.WebViewPage<dynamic>
    View.Title = "Index";
    LayoutPage = "~/Views/Shared/_Layout.cshtml";


I have tried emptying the entire file part by part and nothing seems to help. If I create a controller and view outside the area it works just fine. If I create an aspx view it works fine as well. Is it possible the default razor view engine doesn't support areas at this time?

The areas are registered too.

public static void RegisterRoutes(RouteCollection routes)
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters

Code behinds in MVC2 - How to inject auto-generated code into views via partial classes?

Background:I have written a t4 template that basically replaces the work done by StronglyTypedResourceBuilder to give design time access to string resources as properties of classes, one class per resource file.  Why?  Well I needed to add some customer code that handles string token substitution and a few other customizations.  Anyway, the template is working out really well and I may blog on it soon.In the mean time though, following a common pattern, I have one .resx file for each view, master page, controller, etc.  I would really like my t4 template to add a property to each such entity that gives quick access the custom resource class associated with it.  For controllers this is easy.  T4MVC is ensuring that they are a all declared partial.  All I need to do is create the appropriate partial class in my output that declares a readonly property that returns an instance of the appropriate generated resource class.The Problem:I would like to do the same thing, inject generated code, into my views.  Were this traditional ASP.Net, each .aspx page would have a .aspx.cs page and possibly an .aspx.designer.cs page that are all partial classes extending in the aspx page's class definition.  There is no such thing by default in MVC and for good reason.  However, I think for my purposes, if there is a way to do this, it

MVC3 beta Razor View Base class/inheritance issues


There seem to be a few issues in the way Razor views are designed and the code generated

1. Descendants of WebViewPage are forced to override Execute() (since it is abstract) but that method is never called. Why?

2. The virtual method ConfigurePage is never called in descendants that override this method.

3. CreatePageFromVirtualPath is no longer virtual. This was the one method that gave us access to the Layout Page (Master Page) instance. Now we no longer have this access. Why was this done?

How can one get access to the instance of a Layout page?

C#: Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes


In this article Juval Lowy describes how exciting new features in Visual Studio 2005, that will improve your overall productivity compared to the first version of C#, so you can write cleaner code faster.

Juval Lowy

MSDN Magazine Visual Studio 2005 Guided Tour 2006

C# 2.0: Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes


C# 2.0 introduces a wealth of exiting new features, such as generics, iterators, partial classes and anonymous methods. While generics are the most talked-about feature especially for former classic C++ developers, the rest of the new features are important additions to your .NET development arsenal, enhancing power and improving overall productivity. This article is dedicated to all the new C# 2.0 capabilities besides generics to give you a good overall picture of the upcoming features.

Juval Lowy

MSDN Magazine May 2004

Partial Views and Popups...After clicking on Close need to refresh the page....


Hi All,


I amusing the PopUp example as suggestedin the above forum link.....

But now i want to Refresh the page after clicking on Close(x) ...

So please help me....

Please advice...

Thanks in advance...



Ajax and Validation on Partial Views

I have a main page "Index". Index page has 2 partial views 1. "CategoryList"- list with bunch of checkboxes2. "Add Category" - 1 text field -"BlogCategoryName" with "create" buttonWhat i want to do is when someone types in a new "CategoryName" in the text field of "Add Category" partial view it would send via Ajax to AddCategory controller then it would return a new List and Update "CategoryList" on the index page.Questions:How would i achieve this? so that validation works (ie when CategoryName is blank it would return that partial view with validation) however when successfully validated it would return a new list and update the "CategoryList" partial view.What's the best approeach?would I use JSON or AJAX?thanks

MVC3, Razor, and onkeydown

I am trying to create an extension to extend the HtmlTextbox methods to allow our developers to add onkeydown inline javascript to auto click a button with a name they specify.  The relevent code is:... public static MvcHtmlString(this HtmlHelper htmlHelper, string name, string buttonName, object value, IDictionary<string, object> htmlAttributes { if (htmlAttributes == null) { htmlAttributes = new Dictionary<string, object>() } else { if (htmlAttributes.ContainsKey("onkeydown")) { return htmlHelper.TextBox(name, value, htmlAttributes); } } htmlAttributes.Add("onkeydown", string.Format("if (event.keyCode==13) $('#{0}').click();", buttonName)); return htmlHelper.TextBox(name, value, htmlAttributes); } The problem is that the $('btnTest').click() gets encoded to $(#x39;btn#x39;).click(); So, my question is, in razor, how can we ever put these characters in the attributes of a control (wheter it is a textbox, or any other) if it will always get encoded. OR is this a temporary thing (since it is preview 1), and will be fixed?   chad  

Global navigation and partial views


I guess this is mostly a theory question since I'm very new to MVC.  If you have a global navigation tree, Countries --> States --> Cities for example, how do you correctly implement this in MVC2?  In my example, the view model for States does not contain Countries and the tree would break as it needs the Countries list to build the tree.

It sounds like this should be easy, but it doesn't sound right to include the entire model for a single State or City view.

MVC3 Razor - Option to Not have Html encoding


Since all calls to methods (using Razor View Engine and syntax) that return strings have their result Html encoded, is there a way (or an option) to not have the return values html encoded?

For example if I have a simple method like so:

protected string RenderTable(IEnumerable<T> data)
  /* Return some html */

and then in a view (using the Razor View engine) I have code like this:


I'd like the returned string to be inserted into the view as-is and not Html encoded.

What are my options?

MVC3 Beta requires WebMatrix.Data?


Switching a project from MVC3 Preview 1 to MVC3 Beta 1 and getting the following error when running:

Compiler Error Message: CS0246: The type or namespace name 'WebMatrix' could not be found (are you missing a using directive or an assembly reference?)

This is on the home/index view which is generated from Razor. The generated class is:

Line 1:    #pragma checksum "C:\Dev\Furld\src\Web\Views\Home\Index.cshtml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "7DA68551EE4CE5365FE9A03790329360"
Line 2:    //------------------------------------------------------------------------------
Line 3:    // <auto-generated>
Line 4:    //     This code was generated by a tool.
Line 5:    //     Runtime Version:4.0.30319.1
Line 6:    //
Line 7:    //     Changes to this file may cause incorrect behavior and will be lost if
Line 8:    //     the code is regenerated.
Line 9:    // </auto-generated>
Line 10:   //------------------------------------------------------------------------------
Line 11:   
Line 12: 

MVC3 Razor - Create a instance of a View


In my controller method, I'm trying to create an instance of a View (cshtml file) before I wrap it around an ActionResult and return it.

Since there is no "class" for Razor Views to speak off, how does one go about creating an instance of a Razor View?

MVC3 Beta Interfering with Classic ASP Form Posts


Unfortunately we have classic ASP that must co-exist with MVC in the same site. I'm doing some testing with MVC3 beta and ran into a strange issue with form posts in classic asp that does not appear in the the preview or in prior versions of MVC. To repro the issue simply create a stock MVC3 beta project and run it under IIS7.5 (Not the development server). Then add a couple of classic asp files; one that posts to another:


<form method="post" action="process.asp">
<input type="text" name="yada">

MVC3 Beta Source Code


I cant seem to find the source code for the MVC3 beta release. The source code on the codeplex site hasent been updated in a few months. Is the MVC source repo somewhere else?



Razor View Engine problem in Preview corrected in Beta?



On my projects I am using Razor View engine and I need to define custom Partial folders:

      // Define default view engine
      ViewEngines.Engines.Add(new CshtmlViewEngine());
      ViewEngines.Engines.Add(new WebFormViewEngine());

      // Define Razor view engine
      CshtmlViewEngine razor = (CshtmlViewEngine)ViewEngines.Engines[0];

      // Register partial views folders                
      razor.PartialViewLocationFormats = razor.PartialViewLocationFormats.Concat(new String[] { "~/Views/Partial/{0}.cshtml" }).ToArray();
      razor.AreaPartialViewLocationFormats = razor.AreaPartialViewLocationFormats.Concat(new String[] { "~/Areas/{1}/Views/Partial/{0}.cshtml" }).ToArray();

I had this because there was a problem in MVC 3 Preview. Was it corrected?

How should I register the Partial Views in MVC 3 Beta?

Thank You,


Partial Classes in ASP.net

This link defines the Partial class and usage of partial class with the example.

Partial Classes & Mutliple "Inheritance"


First off, I know C# doesn't allow for multiple inheritance. 

I have an auto-generated partial class, over which I have no control that inherits another object. Like:

public partial class ThisClass : ThatClass

I want to use an abstract class via inheritance in my own partial class to leverage a lot of existing functionality. Like:

public partial class ThisClass : MyAbstractClass
    Method(new AnotherObject(parameter);

public abstract class MyAbstractClass
    protected void Method(AnotherObject parameter)

But how can I? You can't specify multiple inheritance in C#.

I don't think an Interface would work in this scenario? There are many generated partial classes. I just want to use the functionality in my existing library.

If anyone can point to some functional examples of fudging multiple inheritance in the context I provide or suggest a workaround, that would be great!


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend