ReportViewer: culture, number, color and more

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

properties

Step 2. Use the respective Expression, im have:

 CDbl(Fields!SALE_PRICE.Value).ToString("C")



expression


When the report running you have before:


before and after


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:

=Parameters!LanguageInfo.Value
Language-ReportViewer 



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:

IC243124

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:

IC243125

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:

IC243126

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

IC243127

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.


ExpandoObjects


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
.GetType()
.InvokeMember(binder.Name,
BindingFlags.GetProperty
| BindingFlags.Instance
| BindingFlags.Public,
null,
_originalObject,
null);

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)
{
base.SetViewData(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?


Solution


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.

1 

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.

<system.web>

  ....

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

  ....

</system.web>

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.

2

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]

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

    public string Name { get; set; }

    //[Required]

    [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.

3

And here is the output in the browser.

4

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.

5

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.

6

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

<h2>@MvcApplication1.Views.Demo.Greeting.GreetingMsg</h2>

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

7

Angeli e demoni

It takes guts to write a novel that combines an ancient secret brotherhood, the Swiss Conseil Européen pour la Recherche Nucléaire, a papal conclave, mysterious ambigrams, a plot against the Vatican, a mad scientist in a wheelchair, particles of antimatter, jets that can travel 15,000 miles per hour, crafty assassins, a beautiful Italian physicist, and a Harvard professor of religious iconology. It takes talent to make that novel anything but ridiculous. Kudos to Dan Brown (Digital Fortress) for achieving the nearly impossible. Angels & Demons is a no-holds-barred, pull-out-all-the-stops, breathless tangle of a thriller--think Katherine Neville's The Eight (but cleverer) or Umberto Eco's Foucault's Pendulum (but more accessible).

Harvard symbologist Robert Langdon is shocked to find proof that the legendary secret society, the Illuminati--dedicated since the time of Galileo to promoting the interests of science and condemning the blind faith of Catholicism--is alive, well, and murderously active. Brilliant physicist Leonardo Vetra has been murdered, his eyes plucked out, and the society's ancient symbol branded upon his chest. His final discovery, antimatter, the most powerful and dangerous energy source known to man, has disappeared--only to be hidden somewhere beneath Vatican City on the eve of the election of a new pope. Langdon and Vittoria, Vetra's daughter and colleague, embark on a frantic hunt through the streets, churches, and catacombs of Rome, following a 400-year-old trail to the lair of the Illuminati, to prevent the incineration of civilization.

Amazon.co.uk Amazon.it Audio book co.uk 

Angeli e demoni

It takes guts to write a novel that combines an ancient secret brotherhood, the Swiss Conseil Européen pour la Recherche Nucléaire, a papal conclave, mysterious ambigrams, a plot against the Vatican, a mad scientist in a wheelchair, particles of antimatter, jets that can travel 15,000 miles per hour, crafty assassins, a beautiful Italian physicist, and a Harvard professor of religious iconology. It takes talent to make that novel anything but ridiculous. Kudos to Dan Brown (Digital Fortress) for achieving the nearly impossible. Angels & Demons is a no-holds-barred, pull-out-all-the-stops, breathless tangle of a thriller--think Katherine Neville's The Eight (but cleverer) or Umberto Eco's Foucault's Pendulum (but more accessible).

Harvard symbologist Robert Langdon is shocked to find proof that the legendary secret society, the Illuminati--dedicated since the time of Galileo to promoting the interests of science and condemning the blind faith of Catholicism--is alive, well, and murderously active. Brilliant physicist Leonardo Vetra has been murdered, his eyes plucked out, and the society's ancient symbol branded upon his chest. His final discovery, antimatter, the most powerful and dangerous energy source known to man, has disappeared--only to be hidden somewhere beneath Vatican City on the eve of the election of a new pope. Langdon and Vittoria, Vetra's daughter and colleague, embark on a frantic hunt through the streets, churches, and catacombs of Rome, following a 400-year-old trail to the lair of the Illuminati, to prevent the incineration of civilization.

Amazon.co.uk

Advertsing

125X125_06

Planet Xamarin

Planet Xamarin

Calendar

<<  July 2017  >>
MonTueWedThuFriSatSun
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

View posts in large calendar

Month List