Creating custom Html Helper in MVC3

Creating custom attribute is pretty simple really, just follow the example. You need to return MvcHtmlString – that is string with your html to be displayed on a page – this will not be escaped.

public static MvcHtmlString DisplayWithNameFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
{
    ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    if (metaData.Model == null)
    {
        return MvcHtmlString.Create(String.Empty);
    }

    string html = "<div class=\"display-label\">";
    html += htmlHelper.LabelFor(expression).ToString();
    html += "</div><div class=\"display-field\">";
    html += htmlHelper.DisplayFor(expression).ToString();
    html += "</div>";

    return MvcHtmlString.Create(html);
}

Razor view: conditionally include debug versions of JS libraries

In Razor compiler directives do not work, cause Razor Views are not compiled.
So this would not work:

@{ #if DEBUG}

@{#else}

@{#endif}

But you can trick that thing into submission, thanks to this StackOverflow topic

A better, more generic solution is to use an extension method, so all views have access to it:

public static bool IsReleaseBuild()
{
#if DEBUG
    return false;
#else
    return true;
#endif
}

You can then use it like follows in any view (razor syntax):

@if(IsReleaseBuild())

Center Div in CSS

I hate CSS with passion. How many fucking steps do you need to take to make things aligned in the center of a container?
You need to make sure your outside container is set to the required width. Usually 100%.
Then you need to center the loose text by text-align:center;
Then you need to make internal container to have set width (sic!) and give it margins to “auto”.

I understand the concept behind this. But show me a man who came up with idea of setting a margin to auto to be align a container. I’ll shoot him in a face!

blah-bloody-blah! this is centered with no problem.
and this just does not get into center unless you set margins.
blah-bloody-blah! this is centered with no problem.

and this just does not get into center
unless you set margins.