本文介绍了动态类型的ViewPage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这是可能吗?这是我正在尝试的:

  public ActionResult Index()
{
dynamic p = new { Name =Test,Phone =111-2222}; 200新新新新旗新新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗
}

然后我的视图继承自 System.Web。 Mvc.ViewPage< dynamic> 并尝试打印出Model.Name。



我收到一个错误:'<> f__AnonymousType1。名称'由于其保护级别无法访问



所以基本上,我正在尝试做什么是不可能的?为什么或为什么不?



更新:这是我的观点

  ;%@ Page Title =Language =C#MasterPageFile =〜/ Views / Shared / Site.MasterInherits =System.Web.Mvc.ViewPage< dynamic> %GT; 

< asp:Content ...>
<%= Model.Name%>
<%= Model.Phone%>
< / asp:Content>

视图构造函数内置到框架中。

解决方案

匿名类型不能由方法返回;它们仅在定义它们的方法的范围内有效。



您应该使用之前定义的Model类,并将其传递给您的View。传递一个没有定义每个字段的Model类没有错误。



更新



我以前我错了。这应该工作。 X- 200 200 X- 200 200 X- 200 200 X- 200 200 X- 1992:你能发布更多的代码吗?特别是View及其构造函数。



更新第二个



好的,传递匿名键入另一种用作动态变量的方法 - 可以完成。



但是,我也相信你想要做的是工作。不幸的是,你不会。问题是您正在使用 ViewPage< TModel> ,它在内部使用 ViewDataDictionary< TModel> 。因为它们需要强大的类型,所以您将无法使用动态对象。内部结构在内部不使用动态,并且在类型失败时指定动态。



需要一个 DynamicViewPage 类和相应的 DynamicViewDataDictionary 接受对象的类,并将其内部存储为动态。然后你可以使用匿名类型并将其传递给您的Views。



那就是说,你什么也不会收获。您可以根据自己的观点(即<%= Model.Name%> )指定您的观看次数,但您不会受益于强力打字。没有智慧,没有类型的安全。您也可以像@Dennis Palmer所建议的那样使用无类型的 ViewDataDictionary



这是一个有趣的(对我而言,吸收)思想实验,但我认为最终不会发生。声明公共类型并将其传递给您的视图,或使用无类型字典。


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

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

And then my view inherits from System.Web.Mvc.ViewPage<dynamic> and tries to print out Model.Name.

I'm getting an error: '<>f__AnonymousType1.Name' is inaccessible due to its protection level

So basically, is what I'm trying to do just not possible? Why or why not?

Update: here's my view

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ...>
    <%=Model.Name%>
    <%=Model.Phone%>
</asp:Content>

The View constructor is built-in to the framework.

解决方案

Anonymous types cannot be returned by a method; they are only valid within the scope of the method in which they are defined.

You should use a Model class that you have previously defined and pass that to your View. There is nothing wrong with passing a Model class that does not have every field defined.

Update:

I think I was wrong before. This should work. Perhaps the problem is within the View. Can you post more code? Especially the View and its constructor.

Update the Second:

Ok, I was wrong about passing an anonymous type to another method for use as a dynamic variable -- that can be done.

But I was also wrong in my belief that what you're trying to do would work. Unfortunately for you, it will not. The problem is that you are using ViewPage<TModel>, which uses a ViewDataDictionary<TModel> internally. Because they require strong types, you won't be able to use dynamic objects with them. The internal structure just doesn't use dynamic internally, and specifying dynamic as the type fails.

What would be needed is a DynamicViewPage class and corresponding DynamicViewDataDictionary class that accept object and store it internally as a dynamic. Then you could use an anonymous type and pass it to your Views.

That said, you would not gain anything. You would be able to specify your Views as you have done (i.e. <%=Model.Name%>), but you would not benefit from strong typing. There would be no intellisense and there would be no type safety. You'd do just as well to use the untyped ViewDataDictionary as @Dennis Palmer suggests.

This has been an interesting (and, unfortunately for me, absorbing) thought experiment, but I think, ultimately, that it's not going to happen. Either declare a public type and pass it to your Views, or use the untyped dictionary.

这篇关于动态类型的ViewPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:43