Get DisplayName Attribute without using LabelFor Helper in asp.net MVC

<p> <%= Html.Encode( ModelMetadata.FromLambdaExpression<YourViewModel, string>( x => x.SomeProperty, ViewData).DisplayName ) %> <p> Obviously in order to avoid the spaghetti code it is always a good idea to write a helper: public static class HtmlExtensions { public static MvcHtmlString GetDisplayName<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression ) { var metaData = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData); string … Read more

Html helper for

HTML Upload File ASP MVC 3. Model: (Note that FileExtensionsAttribute is available in MvcFutures. It will validate file extensions client side and server side.) public class ViewModel { [Required, Microsoft.Web.Mvc.FileExtensions(Extensions = “csv”, ErrorMessage = “Specify a CSV file. (Comma-separated values)”)] public HttpPostedFileBase File { get; set; } } HTML View: @using (Html.BeginForm(“Action”, “Controller”, FormMethod.Post, new … Read more

MVC 3 – Html.EditorFor seems to cache old values after $.ajax call

There is no caching involved here. It’s just how HTML helper work. They first look at the ModelState when binding their values and then in the model. So if you intend to modify any of the POSTed values inside your controller action make sure you remove them from the model state first: [HttpPost] public virtual … Read more

I want to understand the lambda expression in @Html.DisplayFor(modelItem => item.FirstName)

A lambda expression is a way to write an anonymous function, i.e. a function without a name. What you have on the left side of the “arrow” are the function parameters, and what you have on the right side are the function body. Thus, (x => x.Name) logically translates to something like string Function(Data x) … Read more

Set disable attribute based on a condition for Html.TextBoxFor

The valid way is: disabled=”disabled” Browsers also might accept disabled=”” but I would recommend you the first approach. Now this being said I would recommend you writing a custom HTML helper in order to encapsulate this disabling functionality into a reusable piece of code: using System; using System.Linq.Expressions; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; using … Read more