I need to get all properties name of a model which is init on run time in HtmlHelper extension. I cannot use Type.GetType inside helper because it returns null.
Model Code:
public class SampleVm
{
public object ResultObject { get; set; }
public dynamic ResultDynamic { get; set; }
}
View Code:
@Html.SampleResult(m => m. ResultDynamic) // this is error, I don’t know why
@Html.SampleResult(m => m. ResultObject) // this works
Controller Code:
public ActionResult Index()
{
SearchVm vm = new SearchVm();
vm.ResultObject = Type.GetType("MvcApplication4.Models.Sample.SampleMasterModel");
vm.ResultDynamic = Type.GetType("MvcApplication4.Models.Sample.SampleMasterModel");
return View("Index", vm);
}
HtmlHelper extension Code:
public static HtmlString SearchResult<TModel, TProperty>(this HtmlHelper<TModel> html,
Expression<Func<TModel, TProperty>> expression)
{
TModel model = html.ViewData.Model;
String propertiesName = ?????
// I want to get the properties name from ResultDynamic or ResultObject
// I can found all properties from ResultDynamic in debug but don’t know how to
// get it. For ResultDynamic, I don't know how to get the properties name.
return new HtmlString();
}
I use Type.GetType in help with AssemblyQualifiedName. It solve the problems.
var type = Type.GetType("AssemblyQualifiedName of my Type");
var properties = type.GetProperties();
Thanks Wilson
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.