Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. @Html.LabelFor(m => m.OrganisationId)
  2.  
  3. [DisplayName("OrganisationId *")]
  4. [Required(ErrorMessage = "OrganisationIdis required")]
  5. public string OrganisationId { get; set; }
  6.  
  7. @Html.LabelFor(m=>model.OrganisationId)
  8.  
  9. [Required]
  10. [UIHint("RequiredTemplate")]//this does the trick which should match the file name.
  11. [Display(Name="Organization Id")]
  12. public string Name { get; set; }
  13.  
  14. <font style="color:Red">*</font>@Html.LabelFor(m => m)
  15.  
  16. <div class="display-field">
  17. @Html.DisplayFor(m => m.Name)
  18. </div>
  19.  
  20. public static MvcHtmlString LabelForX<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string requiredText = "required", string innerText = "*" )
  21. {
  22. ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
  23. string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
  24. string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
  25. if (String.IsNullOrEmpty(labelText))
  26. {
  27. return MvcHtmlString.Empty;
  28. }
  29.  
  30. var tag = new TagBuilder("label");
  31. tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
  32. tag.SetInnerText(labelText);
  33.  
  34. if (metadata.IsRequired)
  35. {
  36. var span = new TagBuilder("abbr");
  37. span.Attributes.Add("title", requiredText);
  38. span.Attributes.Add("class", "labelRequired");
  39. span.SetInnerText(innerText);
  40. tag.Attributes.Add("class", "labelRequired");
  41. tag.InnerHtml = span.ToString(TagRenderMode.Normal) + "&nbsp;" + labelText;
  42. }
  43. return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
  44. }
  45.  
  46. @Html.LabelForX(x => x.YourRequieredField)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement