asp.net mvc 2 - Solution to metadata.HideSurroundingHtml not working -
I have written a custom DataAnnotationsModelMetadataProvider that set dynamic HideSurroundingHtml But this editor did not seem to be picked up by templates. So IV labels that try to force HideSurroundingHtml to works created a new HTML extension method But it does not work, except when I engage breakpoints of these, it seems to hit the extension method before CreateMetadata Law - which states that Why this is not working, but the fact is that it is called in this sequence, it does not make sense. Can anyone explain this? Or to achieve what you want to try to achieve?
public class :. DataAnnotationsModelMetadataProvider {Protected Override ModelMetadata CreateMetadata (IEnumerable & lt; Attribute & gt; Properties, type containerType, function & lt; Object & gt; modelAccessor, type modelType, string property) {ModelMetadata metadata = base.CreateMetadata (attributes, containerType, modelAccessor , ModelType, property); Metadata.Hydusurring HTML = True; }}
public static class HtmlCustom {public static MvcHtmlString CustomLabelFor. & Lt; TModel, TProperty & gt; (This HtmlHelper & LT; TModel & gt; HtmlHelper, expression of & lt; function & lt; TModel, TProperty & gt; & gt; expression) {if (HtmlHelperkViewDatakModelMetadatakHideSurroundingHtml) {return MvcHtmlString.Empty; } Return HTML Labelfour (expression); }}
How to do it:
public static MvcHtmlString CustomLabelFor & lt; TModel, TProperty & gt; (This HtmlHelper & lt; TModeel & gt; htmlHelper, expression & lt; Func & lt; TModeel, TProperty & gt; & gt; expression) {var propertyName = ExpressionHelper.GetExpressionText (expression); Var html string = htmlHelper.LabelFor (expression); Model matadata model metadata = html hailer ViewData.Model Metadata Properties Where (x = & gt; x property name == property name). first (); If (modelMetaData.HideSurroundingHtml) {htmlString = MvcHtmlString.Empty; } Return html string; }
There were issues where the HTML was to be taken to HLL. As the beginning of the method for labeling is that CreateMetadata causes fire to do this, in addition to pulling the right meta data, some additional logic was required.