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

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

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

Posted By:      Posted Date: September 16, 2010    Points: 0   Category :ASP.Net
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

View Complete Post

More Related Resource Links

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

My auto-generated code gives errors, can somebody see why???


I have a windowsservice and this windowsservice calls a webservice.
When I update this webservice, visual studio create a reference.cs.
The problem with this reference.cs is that is gives me more then 100 error
The error messages are all the same:
The type name 'AckService' does not exist in the type 'MyNewService.MyNewService' C:\MyNewService\Service References\AckService\Reference.cs 

Can somebody see why?

This is the code:

// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3053
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>

namespace MyNewService.AckService {
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="http://npq/AddIncident", ConfigurationName="AckService.ServiceSoap")]
    public interface ServiceSoap {
        // CODEGEN: Generating message contract since element name xmlString from namespace http://npq/Ad

C++ and ATL: Use ATL Server Classes to Expose Your Unmanaged C++ Code as an XML Web Service


Throughout this issue, you'll read all about the promise of Web Services and how the .NET Framework enables Web Service development. Many people will also be building their Web Services atop C++ code and frameworks like ATL Server, particularly when performance is paramount. In this article, the authors show how fully functional Web Services are built using ATL Server and Visual Studio .NET. Beginning with unmanaged C++ classes, they add ATL attributes that make the code work over HTTP.

Kirk Fertitta and Chris Sells

MSDN Magazine December 2002

How do I change the default HTML code for a collection of lists and views


I have modified the html display code of some lists and content query web parts in SharePoint designer.  This works well when modifying a few lists, but I have many lists and different views with these many lists.

Is there a way to modify the template code for a site so that all my lists, views and CQWPs display identically with my modified code?  Managing these individually is not practical.

auto code generation

Dear All,What are possibilities of automatic code generation using C# and Visual studio 2008?All I know is that there are 3rd party tools like Infragistics and IronSpeed that do this kind of stuff.I don't know if there are any means by which we can do the same i.e generate classes, methods without having to write the code in VS.Has anybody done something like this?If so pls let me know what and how?Thanks.

Getting CSS classes in code

Hi All,I am working on a web application that has multiple themes. The development is parallely being done on all of them. Since I have multiple themes, I have multiple set of CSS files. I doubt that some classes are missing in some of the CSS files.Is there any way to programatically get the CSS classes and check whether all the classes are present in all the files ?Thanks,Girish.

SvcUtil-generated code or shared interface/data contract assembly?

If you control both service and client is there any benefit in using svcutil.exe to generate client proxies? Or is it better to share interface/data contract assembly between client and service?

How to get rid of entitysets in code that is generated using sqlmetal

hiI am using sqlmetal to generate code.below is the line i am using for code generation.SqlMetal.exe  /server:localhost /database:abc /code:..\Entities\CoreEntities.Generated.cs /namespace:abc.DAL /Context:abcDataContext /pluralizeIts working perfect but tables association are also generated , which I dont want in my code.Its generating code for table associations as well.[Association(Name="FK_Profile_LogonInfo1", Storage="_EXPRESS", ThisKey="EXPRESSEDTOUSERID", OtherKey="ID", IsForeignKey=true)]I feel that they are not required in front end, so I dont want sqlmetal to generate code for table associations.Please guide me.thanksSuman

Issue with Proxy code when generated for WCF service



There is an issue I am going through when generating Proxy code for my WCF services. Whenever I generate Proxy code for services with methods having VALUE Type parameters, additional BOOL parameter <parameter>Specified is added to the generate Proxy Code.

So if I have method - Employee GetEmployee(int Id), the respective proxy code for this method will be - Employee GetEmployee(int Id, bool IdSpecified).

Moreover, if you do not set this bool parameter value to TRUE, no value will be passed to the End service.

I can understand this can come handy when handling Null inputs, but that will not be always the case.

After doing some R&D, I found that if I add Data Format attribute of type RPC to my Method contract, these parameters are removed. Like if I add: 

[DataContractFormat(Style = OperationFormatStyle.Rpc)] , the generated proxy code reflects all value type parameters as is.

I am not an expert in WCF, and for the same reason I don't have much idea on what significance this attribute plays for the service methods. Can adding this attribute introduce problems when consuming these methods by application in .NET 1.1 or in JAVA?


Can anyone help me in getting ride of this additional parameter in the proxy?

Thanks in Advance.




Reference to a non-shared member requires an object reference in code generated by ASP.NET



I am working on converting a project from C# to VB and am facing this weird issue. 1 page cannot compile and I get the reference error, but the weird thing is that the compiler shows the error as coming from ASP.NET generated code. Visual Studio shows me that error is coming from my ASPX page. I cannot seem to figure out why or where this error is occuring. Also if I take the inherits attribute out I don't get the compilation error. The code in aspx page is below:

<%@ Page Language="VB" AutoEventWireup="true" CodeBehind="diff.view.aspx.vb"  Inherits="MR.ControlApp.diffview" MasterPageFile="~/Site1.Master"%>

<asp:Content ID="Header1" ContentPlaceHolderID="head" runat="server">

    Page: <select onchange="sel(this);">
        Dim j As Integer = 0
        While j < pageCount
           <option <%= IIf(currentPage = ( j + 1 ), "selected", "")  %> value="<%= j+1 %>"><%= j + 1 %></option>
        <%j = j + 1
        End While
    <% 'Print(); %>
    <div style="font-family: verdana; font-size: 10pt; font-weight: bold">
        MDM Data Inconsiste

Is the executable file generated by compiler a Managed code or Unmanaged code ?



when we compile our vb.net or c#.net code (say in a simple console application) then in bin\Debug folder a .exe file is created.Is that a managed code? when i directly execute this file,will this target CLR ? or will it directly run on OS ?

Why there is ConsoleApplication1.vshost.exe.manifest files created in that folder ?

What is .pdb file ?

That executable file is also created in obj\Debug folder .Why ?

MVC3 Beta Razor Views - not partial classes


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.

VS2010 - Problem with WinForms generated code



I'm having trouble with the code that is generated automatically by the WinForms designer, sometimes it generates code that uses the 'components' (IContainer) in the constructors of disposable components and other times it doesn't.

Here is some code. First the code that is generated in a clean new project with just one component on the form:

  private void InitializeComponent()
   this.components = new System.ComponentModel.Container();
   this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
   // errorProvider1
   this.errorProvider1.ContainerControl = this;
   // Form1

Namespace Issue in Generated Code


I have a Solution in which I have A WCF Service web site project which then hosts two separate WCF Service Libraries (each in their own project).  One of the Web Services is a Client to the other. 

When I click the ClientService and select Add Service Reference, it all adds properly, but when I go to build it, I get this error in the reference.cs (the generated file).

The type name 'HostWCFService' does not exist in the type 'ClientService.ClientService' C:\Users\...\Reference.cs 

I don't want it to create a ClientService.HostWCFService namespace, I want to be able to use the HostWCFService as its own namespace.  I believe I've had this setup working before at one point; that is, I had this arrangement working before, but am not sure now if the host service was in a sub namespace of the project.

I was able to do: 

using HostWCFService;

instead of

using ClientService.HostWCFService;

Any thoughts? 

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