rmariuzzo / Mariuzzo.Web.Mvc.Extras

Extra missing components for ASP.NET MVC.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mariuzzo.Web.Mvc.Extras

Extra missing components for ASP.NET MVC.

Installation

To install Mariuzzo.Web.Mvc.Extras, run the following command in the NuGet's Package Manager Console

Install-Package Mariuzzo.Web.Mvc.Extras

Documentation

ExpressionHelper

GetExpressionText

What is inside the Extras?

ExpressionHelper

A better ExpressionHelper class that properly handles UnaryExpression (aka Convert expression).

How to use

On any code block just call the static methods: Mariuzzo.Web.Mvc.Extras.ExpressionHelper.GetExpressionText(...).

The Problem

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.

Solution

The Mariuzzo.Web.Mvc.Extras.ExpressionHelper provides a GetExpressionText method that handle that specific case.

ModelStateDictionaryExtensions

Set of extension methods for ModelStateDictionary providing methods supporting Lambda Expression instead of magic string.

How to use

On top of any of your Controllers just add: using Mariuzzo.Web.Mvc.Extras;.

The Problem

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.

Solution

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))
{
	...
}

About

Extra missing components for ASP.NET MVC.


Languages

Language:C# 100.0%