简体   繁体   English

ASP.NET MVC 2 DisplayFor()

[英]ASP.NET MVC 2 DisplayFor()

I'm looking at the new version of ASP.NET MVC (see here for more details if you haven't seen it already) and I'm having some pretty basic trouble displaying the content of an object. 我正在查看ASP.NET MVC的新版本(如果您还没有看到它,请参见此处以了解更多详细信息),并且在显示对象内容时遇到一些非常基本的麻烦。

In my control I have an object of type Person , which I am passing to the view in ViewData.Model . 在我的控件中,我有一个类型为Person的对象,该对象将传递给ViewData.Model的视图。 All is well so far and I can extract the object in the view ready for display. 到目前为止一切都很好,我可以在视图中提取对象以准备显示。 What I don't get though, is how I need to call the Html.DisplayFor() method in order to get the data to screen. 不过,我没有得到的是如何调用Html.DisplayFor()方法才能将数据显示到屏幕上。 I've tried the following... 我尝试了以下方法...

<% 
    MVC2test.Models.Person p = ViewData.Model as MVC2test.Models.Person;
%>
// snip
<%= Html.DisplayFor(p => p) %>

but I get the following message: 但我收到以下消息:

CS0136: A local variable named 'p' cannot be declared in this scope because it would give a different meaning to 'p', which is already used in a 'parent or current' scope to denote something else CS0136:无法在此范围内声明名为“ p”的局部变量,因为它将赋予“ p”不同的含义,“ p”已在“父级或当前”范围中用于表示其他内容

I know this is not what I should be doing - I know that redefining a variable will produce this error, but I don't know how to access the object from the controller. 我知道这不是我应该做的-我知道重新定义变量会产生此错误,但是我不知道如何从控制器访问对象。 So my question is, how do I pass the object to the view in order to display its properties? 所以我的问题是,如何将对象传递给视图以显示其属性?

NB I should add that I am reading up on this in my limited spare time, so it is entirely possible I have missed something fundamental. 注意:我还要补充一点,我会在有限的业余时间里阅读这些内容,因此我完全有可能错过了一些基本知识。

TIA TIA

Html.DisplayFor can be used only when your View is strongly-typed, and it works on the object that was passed to the View. Html.DisplayFor仅在您的View是强类型的,并且对传递给View的对象起作用Html.DisplayFor可以使用。

So, for your case, you must declare your View with the type Person as its Model type, (eg something.something.View<Person> ) (sorry, I don't remember the exact names, but this should make sense), and then when calling Html.DisplayFor(p => p) , p would take the value of the passed model (Person) value into the view. 因此,对于您的情况,您必须声明类型为Person的View作为其Model类型(例如, something.something.View<Person> )(对不起,我不记得确切的名称,但这应该是有道理的),然后在调用Html.DisplayFor(p => p)p会将传入的模型(Person)值的值带入视图。

Hope that made sense. 希望有道理。

p is already a variable name; p已经是一个变量名; and variable names have to be unique throughout the current scope. 并且变量名称在当前范围内必须唯一。 Therefore displayFor(p=>p) is not valid, as you're declaring a new variable 'p' there. 因此displayFor(p => p)无效,因为您在此处声明了新变量'p'。 That way the compiler doesn't know wether to use your Person p, or the (p =>) variable. 这样,编译器就不知道是否要使用您的Person p或(p =>)变量。

So just rename it to 因此,只需将其重命名为

<%= Html.DisplayFor(person => person) %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM