Showing posts with label mvc. Show all posts
Showing posts with label mvc. Show all posts
Friday, April 24, 2015
Thursday, January 19, 2012
Replacement for LabelFor in ASP.NET MVC 3
As I've been deep-diving further into ASP.Net MVC 3, I ran into an issue where I needed to arbitrarily show an indicator against a label that would tell me which fields are required. We were initially doing this by hand, but have been using a ton of inheritance to set [Required] attributes on fields. In order to overcome this pain, we decided to create a helper method that we now use instead of @Html.LabelFor()... it looks like this (Exactly from *.cs file):
Hope that this helps someone...
using System.Linq;
using System.Linq.Expressions;
namespace System.Web.Mvc
{
public static class HtmlHelpers
{
public static MvcHtmlString LabelForRequired<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression,
string id="", bool generatedId=false)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
if (metadata.IsRequired)
{
var labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (string.IsNullOrWhiteSpace(labelText))
{
return MvcHtmlString.Empty;
}
var tag = new TagBuilder("label");
var spanTag = new TagBuilder("span");
spanTag.AddCssClass("required");
spanTag.SetInnerText("*");
if (!string.IsNullOrWhiteSpace(id))
{
tag.Attributes.Add("id", id);
}
else if (generatedId)
{
tag.Attributes.Add("id", htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName) + "_Label");
}
tag.Attributes.Add("for", htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
tag.SetInnerText(labelText);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal) + " " + spanTag.ToString(TagRenderMode.Normal));
}
else
{
return System.Web.Mvc.Html.LabelExtensions.LabelFor(htmlHelper, expression);
}
}
}
}
Hope that this helps someone...
Thursday, November 3, 2011
More quirks with ASP.NET MVC 3
So, another great quirk with ASP.NET MVC 3. When you implement a controller with two methods, one being for the initial load of the page, and the other being for the subsequent form post to process the data... if your intention is to re-render the page to do another 'Add Item' scenario, you must issue a ModelState.Clear(); command before you render the new view... otherwise, it seems to keep the values from the posted data.
Monday, June 27, 2011
ASP.NET MVC3 has a few quirks
http://aspnet.codeplex.com/workitem/7629
Ack! Seems like this shouldn't have happened.
To correct, you have to do the following:
Give your select list the "Required" attribute.
Once the page loads, in jQuery, assign the data-val attribute and the data-val-message attributes.
Microsoft, this is not good!
Ack! Seems like this shouldn't have happened.
To correct, you have to do the following:
Give your select list the "Required" attribute.
Once the page loads, in jQuery, assign the data-val attribute and the data-val-message attributes.
Microsoft, this is not good!
Subscribe to:
Posts (Atom)