ReportViewer: culture, number, color and more

Step 1. Set the "Language" report properties according to your regional requirements. In my case: "es-MX"


Step 2. Use the respective Expression, im have:



When the report running you have before:

before and after


Background Color

If you want that the alternate row in a table will be other color, you have to click on the row on Background Color and insert the following expression:

=IIF(RowNumber("Statements") Mod 2, "White", "WhiteSmoke")

Change Language

If you can change the language of a report it is necessary that you create a parameter (for example called LanguageInfo) and then on the property of the report in the Language key, you need to insert this:


How to repeat table's header rows on each page with no row groups

By default, a simple table with no groups does include the details group. The following figure shows a table that displays category, order number, and sales data:


There are no dotted lines because the table consists only of the tablix body area. The first row displays column headers, and represents a static tablix member that is not associated with a group. The second row displays detail data, and represents a dynamic tablix member that is associated with the details group. The following figure shows the table in preview:


To repeat or freeze column headings, set properties on the tablix member for static row that is part of the tablix data region definition. To select the static row, you must use the Advanced mode of the Grouping pane. The following figure shows the Row Groups pane:


In Advanced mode, the following figure shows the static and dynamic tablix members for the row groups in the table:


To repeat or freeze column headings for the tablix member, select the static row that is labeled (Static). The properties pane displays the properties for the selected tablix member. By setting properties for this tablix member, you can control how the first row repeats or stays in view.

Anonymous View Models in ASP.NET MVC Using Dynamics

The introduction of the dynamic keyword in C# 4.0 allows you to do a lot of things you couldn’t do otherwise, but it also makes it easy to forget that C# is still not a dynamic language, so there are limitations. Recently I found myself wanting to whip up a quick test page for something, with a very simple controller action and view. It wasn’t meant to be permanent, so creating a dedicated view model class in the project seemed like overkill. I figured I could just return an anonymous object as the model, and use a dynamic view to display it. I was wrong.

The Limits of Anonymous Classes

Let’s take a look at a quick example to demonstrate the problem. Here is my very simple controller action, that uses an anonymous class as the view model:

public ActionResult UsingDynamic()
return View(new
TestString = "This is a test string"

Now let’s make sure the data gets sent correctly to the view. First we’ll try just printing the model to the page using its ToString() method:

<%: Model %>

We get the output:

{ TestString = This is a test string }

So far so good! We see an object that has the TestString property and value we wanted. Now let’s try printing out the value of TestString:

<%: Model.TestString %>

This time ASP.NET throws an exception back at us, saying “‘object’ does not contain a definition for ‘TestString’”. How can this be? When we printed the object we could clearly see it had a property named TestString, and had a value for it. The answer here lies in the fact that anonymous classes are internal by default, meaning they are only accessible to the assembly in which they were created. Once you cross that assembly boundary, it gets treated as a regular object, which does not have the TestString property.

So if anonymous classes are internal, how can we accomplish what we want? There are a few ways I could think of.

Use a POCO

I know I said this was the solution I was trying to avoid, but I thought it was worth mentioning because in a majority of cases, having a well defined view model is a good choice. To wire this up using a defined view model, it would look something like this:

public class PocoViewModel
public string TestString { get; set; }

And the controller action:

public ActionResult UsingPoco()
return View(new PocoViewModel()
TestString = "This is a test string"

The view would just need to inherit from ViewPage and it would work as expected.


Another new addition in .NET 4.0 is the ExpandoObject. An ExpandoObject is an object that allows for dynamically adding and removing members at runtime. Since that is its purpose, it gets us something pretty similar to what we were trying to achieve with the anonymous class, but it will make it over the assembly boundary. The controller action using an ExpandoObject would look like:

public ActionResult UsingExpando()
dynamic viewModel = new ExpandoObject();
viewModel.TestString = "This is a test string";

return View(viewModel);

The view should inherit from ViewPage (which is now actually the default), and it would be able to successfully access the TestString property. This is actually pretty much what they’re doing in ASP.NET MVC 3 with the new ViewModel property in Controller.

Dynamic Reflection

But what if you don’t want to use an ExpandoObject, and just want to throw an anonymous class at your view and have it just work? You can actually make this happen with an impressively small amount of code. First, we’ll create a custom dynamic class that wraps another object instance, and uses reflection to provide property access to that instance. To do that, the class will extend the DynamicObject class, which handles most of the heavy lifting for dealing with dynamics. If we wanted to do everything outselves, we could implement IDynamicMetaObjectProvider outselves (which DynamicObject does), but in this case there’s no real reason to, so why not let DynamicObject do the hard work for us?

public class DynamicPageObject : DynamicObject
private object _originalObject;

public DynamicPageObject(object originalObject)
_originalObject = originalObject;

public override bool TryGetMember(GetMemberBinder binder, out object result)
result = _originalObject
| BindingFlags.Instance
| BindingFlags.Public,

return true;

There we override the TryGetMember(…) function of DynamicObject, to allow it to use reflection over the original object to provide its member access. It’s pretty simple reflection, and that’s all there is to it.

Next we’ll create our own class that extends ViewPage that will automatically wrap the model in a DynamicPageObject:

public class DynamicPage : ViewPage
public new dynamic Model { get; set; }

protected override void SetViewData(ViewDataDictionary viewData)

Model = new DynamicPageObject(viewData.Model);

We override the base Model property to use a dynamic model, which gets set to a DynamicPageObject that wraps around the original model. That’s all the code we need to make this work, so let’s give it a shot. The controller action is identical to the one from the original example, using an anonymous class:

public ActionResult UsingCustomDynamic()
return View(new
TestString = "This is a test string"

For the view, the only thing we need to do is have it inherit from DynamicPage and we’re done. Now we have a controller passing in an anonymous class as a view model, and a view that can handle that dynamically.

Why does Html.ActionLink render “?Length=4”

I'm VERY confused as to why this code

Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })

results in this link:

<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>

The hidefocus part is what I was aiming to achieve, but where does the "?Length=4" come from?


The Length=4 is coming from an attempt to serialize a string object. Your code is running this ActionLink method:

public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)

This takes a string object "Home" for routeValues, which the MVC plumbing searches for public properties turning them into route values. In the case of a string object, the only public property is Length, and since there will be no routes defined with a Length parameter it appends the property name and value as a query string parameter. You'll probably find if you run this from a page not on HomeController it will throw an error about a missing About action method. Try using the following:

Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })

Globalization and Localization of Model Validation Messages in MVC

In this quick article you will learn how to globalize and localize the model validation messages in MVC Applications. At the end of this article, you will also learn how to display the localized message (greeting) on the page.

Here is what we will build.


I will use ASP.NET resource files to define the globalized (default) and localized strings and this string message will be displayed to the user based on the request header language that the client browser sends.

Now, follow the steps from here.

Step 1

To do ASP.NET automatically we need to set the globalization settings in web.config, as in the following.



  <globalization culture="auto" uiCulture="auto"/>



Step 2

To localize the text we need to rely on the resource files that are nothing but a XML file with .resx extension that contains globalized and localized string messages, name and its value pair.

These resource files can be added anywhere in the application but I will prefer to add it with views.


Error.resx File

In this file I have defined two string names AddressRequired and NameRequired. This file can be called a default/base resource and will be displayed every time the browser requests a language that we don't support. Remember to change the access modifier to Public, because this file turns into an assembly that requires public access.

Error.hi.resx File

This file will have the localized name and value pair, which is Hindi here. This string message will be displayed to the user based on the request header language that the client browser sends.

Step 3

To display these localized validation messages, we need to make a quick change in the model validation attribute, here we go.

public class DemoFoolProofModel


    public int Id { get; set; }


    [Required(ErrorMessageResourceType=(typeof(MvcApplication1.Views.Demo.Error)), ErrorMessageResourceName="NameRequired")]

    public string Name { get; set; }


    [Required(ErrorMessageResourceType=(typeof(MvcApplication1.Views.Demo.Error)), ErrorMessageResourceName="AddressRequired")]

    public string Address { get; set; }


I am overriding the validation message displayed by [Required]. ErrorMessageResourceType will have the name of resource file and ErrorMessageResourceName will have the name of the name-value pair. That's it.

Step 4

Now we are all set to run the application, hit F5, at the same time open Fiddler to inspect the request header. I selected English (United States) as a primary language and you can see Fiddler has "en-US" in the request header. In this case the Error.resx file information will be rendered.


And here is the output in the browser.


Step 5

Let's select Hindi as a primary language and you will see Fiddler has "hi" in the request header. In this case the Error.hi.resx file information will be rendered.


Step 6

In the same way if you want to display some greeting on your website then you can do this by adding another two resource files, as in the following.


Now, to display this localized message on the view page use the following code:


Now, select Hindi as a primary language, you will see it working.