1

HtmlHelper 拡張機能で実行時に初期化されるモデルのすべてのプロパティ名を取得する必要があります。null を返すため、ヘルパー内で Type.GetType を使用できません。

モデルコード:

public class SampleVm
{
    public object ResultObject { get; set; }

    public dynamic ResultDynamic { get; set; }
}

コードを表示:

@Html.SampleResult(m => m. ResultDynamic)    // this is error, I don’t know why
@Html.SampleResult(m => m. ResultObject)   // this works

コントローラーコード:

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 拡張コード:

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();
}
4

1 に答える 1

1

私は Type.GetType を AssemblyQualifiedName の助けとして使用しています。それは問題を解決します。

var type = Type.GetType("AssemblyQualifiedName of my Type");

var properties = type.GetProperties();

ありがとうウィルソン

于 2013-11-12T09:27:59.637 に答える