Extra missing components for ASP.NET MVC.
To install Mariuzzo.Web.Mvc.Extras, run the following command in the NuGet's Package Manager Console
Install-Package Mariuzzo.Web.Mvc.Extras
A better ExpressionHelper
class that properly handles UnaryExpression
(aka Convert expression).
On any code block just call the static methods: Mariuzzo.Web.Mvc.Extras.ExpressionHelper.GetExpressionText(...)
.
The System.Web.Mvc.ExpressionHelper#GetExpressionText
return an empty string for any Nullable
property.
If you have the following Foo
class:
class Foo {
public int? Id { get; set; }
}
And then run: System.Web.Mvc.ExpressionHelper.GetExpressionText((Foo f) => f.Id)
you will receive an empty string instead of: Id
. The same goes for any inner property that are Nullable
.
The Mariuzzo.Web.Mvc.Extras.ExpressionHelper
provides a GetExpressionText
method that handle that specific case.
Set of extension methods for ModelStateDictionary providing methods supporting Lambda Expression instead of magic string.
On top of any of your Controllers just add: using Mariuzzo.Web.Mvc.Extras;
.
What would happen to the following code if you rename the property Bar
of your model?
if (ModelState.IsValidField("Bar")
{
...
}
If you forgot to also rename any of those magic string, I'm pretty sure you and your controllers will be in problem.
The Mariuzzo.Web.Mvc.Extras.ModelStateDictionaryExtensions
extend the legacy ModelStateDictionary and provides the same methods, but instead of magic strings it accepts Lambda Expression.
if (ModelState.IsValidField((Foo f) => f.Bar))
{
...
}