Dynamic typed ViewPage

后端 未结 4 1110
无人及你
无人及你 2020-12-30 07:14

Is this possible? Here\'s what I\'m trying:

    public ActionResult Index()
    {
        dynamic p = new { Name = \"Test\", Phone = \"111-2222\" };
                


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 07:55

    The actual error here (<>f__AnonymousType1.Name' is inaccessible due to its protection level) is the result of using anonymous types. Anonymous types are implicitly internal (at least in C#), therefore they can only be accessed normally from the same assembly. Since your view is compiled into a separate assembly at runtime, it can't access the internal anonymous type.

    The solution is to pass concrete/named classes as models to your view. The view itself can still use dynamic if you want.

提交回复
热议问题