如何从视图访问模型? [英] How to access the Model from the View?

查看:50
本文介绍了如何从视图访问模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示搜索结果的搜索页面.搜索结果是与特定搜索匹配的人员列表.我正在遍历此列表,将它们显示在表中.作为此表的标题,我需要模型的DisplayName.如果我不继承IEnumerable,那么我将无法遍历该列表.我是MVC的新手=)

I have a search page that display a search result. The search result is a list of persons that matched the specific search. I'm iterating through this list displaying them in a table. As headers for this table I want the DisplayName from the model. If I don't inherit IEnumerable I wouldn't be able to iterate through the list. I'm new at this MVC thing =)

我这样遍历结果:

<% foreach (var item in Person) { %> 
   <%: item.surname %>
<% } %>

但是,如何在不迭代整个列表的情况下打印属性的"DisplayName"?我只想这样做:

But how do I print the "DisplayName" of an attribute without iterating through the whole list? I would just like to do:

<%: Html.LabelFor(m => m.surname) %>

如果有帮助,我可以在页面顶部继承IEnumerable:

If it's any help I inherit an IEnumerable at the top of the page:

<%@ Page Inherits="System.Web.Mvc.ViewPage<IEnumerable<RegistryTest.Models.Person>>" %>  

修改
我想显示您的姓氏",但我不知道如何从视图中访问它.

Edit
I want to display "Your surname" but I don't know how to access it from the view.

[DisplayName("Your surname")]
public object surname { get; set; }  

这是一个非常相似的问题,也没有得到回答:

Here's a very similar question that hasn't been answered either: Can I use LabelFor() when the page inherits from IEnumerable<T>?

推荐答案

如果只需要显示一个人的详细信息,请显示;您应该考虑只发送一个人到视图,而不是完整的人列表.在这种情况下

If you only need to display specifics of one person; you should consider sending only one person to the view instead of a complete list of persons. In that case

Model.Surname

将像那样工作.因此,代替:

would work just like that. So instead of:

Inherits="System.Web.Mvc.ViewPage<IEnumerable<RegistryTest.Models.Person>>"

Inherits="System.Web.Mvc.ViewPage<RegistryTest.Models.Person>"

在这种情况下,将一个人加载到您的模型和模型中.属性可以正常工作.如果您想要IENumerable<>,请考虑一下这是什么意思.您正在发送人员列表,因此模型"中唯一的内容就是可数的人员.如果调用Model.Property,则视图无法知道您想要的内容,因为在模型中有多个对象,而视图也不知道您要从哪个对象获取属性.

In this case a single person is loaded into your Model and Model.Property works fine. If you want an IENumerable<>, think about what that means. You are sending a list of persons, so the only thing in your "Model" is a IENumerable<> of persons. There is no way that the view can know what you want if you call Model.Property, since in the Model there are multiple Objects and the view doesn't know which Object you want to get the Property from.

最重要的是,如果您想调用Model.Property(Model.surname),但又想发送一个IENumerable,则您存在设计缺陷.如果发送列表,则应该对完整列表进行处理(反复浏览并对内容进行处理).如果您只想与该列表中的一个人一起做某事,请重新设计视图/控制器并发送给该单独的人;那么您可以使用Model.Property.

Bottom line, if you want to call Model.Property (Model.surname) but also want to send an IENumerable you are having a design flaw. If you send a list you should want to do something with the complete list (iterate through and do something with the contents). If you just want to do something with one person in that list, re-design your view/controller and send that single person; then you can use Model.Property.

//基于编辑的评论正如我现在所看到的,您要么想做这两件事之一(我不知道是哪一个):

//EDIT BASED UPON COMMENTS As I see it now you either want to do one of those two things (I do not know which):

  1. 在表的列表中显示项目的记录,并将表中显示的当前对象的DisplayName放在标题中.
  2. 显示表中列表的所有项目,并将某种DisplayName放在标题中.这样的话意义不大,但可能是您想给列表起名字.

情况1 这与您的其余代码一起工作吗?以下将正常工作.

Situation 1 This is working as the rest of your code? The following would work just fine.

<% foreach (var item in Model) { %> 
   <table>
      <th>item.DisplayName</th>
      <tr>
         <td>item.Property1</td>
         <td>item.Property2</td>
      </tr>
   </table>
<% } %>

情况2 如果要使用列表的DisplayName(??),则应创建一个包含IENumerable of Persons和公共字符串ListName的ViewModel.现在您可以执行以下操作:

Situation 2 If you want a DisplayName of the list (??) you should create a ViewModel containing the IENumerable of Persons and a public string ListName. Now you can do something like:

<table>
  <th>Model.ListName</th>
  <% foreach (var item in Model) { %>           
      <tr>
         <td>item.Property1</td>
         <td>item.Property2</td>
      </tr>
  <% } %>
</table>

这将创建一个表,其中具有您的List的名称(在ViewModel中给出)作为标题,并且作为您表中的人员.

this would create a table with the name of your List (given in the ViewModel) as header and as items in the table you have your persons.

设计问题?但是,我希望您能在上面的问题中写出更多信息.给我们一些有关您想做什么的信息.您是否要一一列出清单中每个人的记录?在这种情况下,我建议您在放置桌子的地方创建一个 Partial View .然后您会得到类似的东西:

Design problem? However, I would love to see you write some more information in your question above. Give us some more information on what you want to do. Do you want to show records of each Person in the list one-by-one? In that case I would recommend you create a Partial View where you put your table. Then you would get something like:

<% foreach (var item in Model) { %>     
   <% Html.RenderPartial("TablePerson",item); %>
<% } %> 

tableperson.ascx:

tableperson.ascx:

...
Inherits="System.Web.Mvc.ViewPage<IEnumerable<RegistryTest.Models.Person>>"
...
<table>
   <th>Model.DisplayName</th>
   <tr>
      <td>Model.Property1</td>
      <td>Model.Property2</td>
   </tr>
</table>

所以,恐怕我们需要更多信息:)

So, we need more information I'm afraid :)

这篇关于如何从视图访问模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆