How to extend MVC3 Label and LabelFor HTML helpers?

You can easily extend the label by creating your own LabelFor: Something like this should do what you need public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) { return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = … Read more

Html inside label using Html helper

Looks like a good scenario for a custom helper: public static class LabelExtensions { public static MvcHtmlString LabelFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, Func<object, HelperResult> template ) { var htmlFieldName = ExpressionHelper.GetExpressionText(ex); var for = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName); var label = new TagBuilder(“label”); label.Attributes[“for”] = TagBuilder.CreateSanitizedId(for); label.InnerHtml = template(null).ToHtmlString(); return MvcHtmlString.Create(label.ToString()); } } and then: … Read more

Razor reseverd words

Here’s a list of Razor reserved keywords (Note: This applies to cshtml, vbhtml follows VB’s rules): Razor-specific keywords inherits functions section helper model (only in MVC projects) You can escape these using @(inherits) Language-specific Razor keywords These are C# keywords that are understood by Razor if do try for foreach while switch lock using case … Read more

Could not load file or assembly ‘System.Data.Entity

To use an external Entity Framework model (embed in a DLL for example) with ASP.NET MVC 3 you must : Add the following reference to your MVC project : System.Data.Entity (Version 4.0.0.0, Runtime v4.0.30319) Add the following line in your web.config … < compilation debug=”true” targetFramework=”4.0″> <assemblies> <add assembly=”System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ /> </assemblies> </compilation>

Ninject Binding Attribute to Filter with Constructor Arguments

I have figured it out (thanks to Remo’s directions and documentation). Use the appropriate .WithConstructorArgument extension whether you are binding to a Controller or Action filter. For example binding my action filter looks like this: kernel.BindFilter<AuthorizationFilter>(FilterScope.Action, 0) .WhenActionMethodHas<RequireRolesAttribute>() .WithConstructorArgumentFromActionAttribute<RequireRolesAttribute>(“requiredRoles”, o => o.RequiredRoles); Once I understood the Func<> signature, it all became clear. The best way … Read more

How to specify ID for an Html.LabelFor (MVC Razor) [duplicate]

Unfortunately there is no built-in overload of this helper that allows you to achieve this. Fortunately it would take a couple of lines of code to implement your own: public static class LabelExtensions { public static MvcHtmlString LabelFor<TModel, TValue>( this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes ) { return LabelHelper( html, ModelMetadata.FromLambdaExpression(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), … Read more