I have a custom My helper, and I would like to get the value of the property used in it.
I call it this way:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<VMThatHasAllTheseProps>" %>
<%=Html.My("Hi") %>
<%=Html.EditorFor(o => o.Prop) %> \\uses Html.My in the My.ascx template
<%=Html.My("[0].My") %>
<%=Html.My("[1].My") %>
and assistant:
public static MvcHtmlString My(this HtmlHelper html, string prop, object value = null)
{
}
At the moment I like it:
if (value == null && html.ViewData.Model != null)
{
var p = TypeDescriptor.GetProperties(html.ViewData.Model).Find(prop, false);
if (p != null) value = p.GetValue(html.ViewData.Model);
}
if(value == null && html.ViewData.ModelMetadata != null)
{
var p = TypeDescriptor.GetProperties(html.ViewData.ModelMetadata.Model).Find(prop, false);
if (p != null) value = p.GetValue(html.ViewData.ModelMetadata.Model);
}
if (value == null && html.ViewData.ContainsKey(prop))
value = html.ViewData[prop];
but obviously this does not work for "[0].My"
source
share