Home » C#

It’s possible to create a custom generic Html Extension Helper with the use of Expression Helper and if required the ModelMetadata to get the value. public static string MyExtensionFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> property) { var meta = ModelMetadata.FromLambdaExpression(property,...
Filed in: ASP.MVC, C#
Tim Barcz, Matt Hawley, Stephen Walther and Scott Guthrie (VP at Microsoft and lead for many project like Entity Framework, Asp.Net, etc) have already discussed about this problematic and created the PRG pattern to solve this problem. In fact, to solve this problem you should not handle manually the ModelState but simply use Import and Export attribute...
Filed in: ASP.MVC, C#
Sometime you may need to store information for few times in the memory without having to compute the data every time a user request it. This can save time and also processor time. To do, we can use the System.Web.Cache object. var cc = HttpRuntime.Cache.Get("Test"); DateTime t; if (cc == null) { t = DateTime.Now; HttpRuntime.Cache.Insert("Test",...
Filed in: ASP.MVC, ASP.Net, C#
It’s really easy to have in ASP.NET MVC a function that return an anonymous type. I say in ASP.NET MVC but this could be also in ASP.NET. In fact, when you have an action inside a controller that return a JsonResult you can simply return an anonymous type and Javascript will be able to handle it as simple as using the same syntax that you would...
Filed in: ASP.MVC, C#, dynamic
If you are using a collection : ICollection, IList, List, which doesn’t use enumerator to navigate, the fastest way to check if the collection contain anything is to use .Length or .Count. But, if you are using IEnumerator which contain GetEnumerator() and MoveNext() the fastest way is to use .Any(). The reason is that Any() will do 1 loop and...
Filed in: C#
I had to use a method that with reflection would take the properties name and value to create a SQL query. The problem is that the library was limited to methods directly inside the object passed and not value of inner object. For example, if the class A contain an object B, we were not able to access B properties. We could have done something pretty...
Filed in: C#
The interface INotifyPropertyChanged require to pass has an event called event PropertyChanged that take a String as parameter. This String represent the name of the property changed. This kind of behaviour exist in many other system. Recently, I had to use a small project called Peta Poco, which is a tiny-tiny-tiny ORM for .Net. This also require...
Filed in: C#
It’s possible with .Net to write data into Excel’s cells. This can be done by using the library of Microsoft.Office. The first step is to add a reference into you project by right clicking on the References folder and add the COM of Microsoft Office Excel. Here is a small example that show you how to open an Excel worksheet and add value...
Filed in: C#
If you came from the world of NUnit you may stomped into the error “Method should be marked static” without any other indication of which method it’s about. With NUnit, you were using TestFixtureSetUp as attribute to a class’ method to get it executed before all test once. With MSTest you have to use the attribute ClassInitialize. So,...
Filed in: C#
When unit testing you can get this kind of error if you try to use a class that use different thread of the main one. At first, I haven’t realized that I were using another thread because I was testing my Shape class. After some minutes, I realized that I were using the System.Windows.Shape and not mine… once the reference set correctly...
Filed in: C#
wordpress themplates