【问题标题】:How to display the List of object to view如何显示要查看的对象列表
【发布时间】:2018-01-07 00:58:50
【问题描述】:

我的模型类

 public class DisplaceModel
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Type { get; set; }
        public string Rating { get; set; }
        public string PhotoReference { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
    }

public class DisplaceModelInformation
    {
        public List<DisplaceModel> Dispaylist { get; set; }
        public DisplaceModelInformation()
        {
            Dispaylist = new List<DisplaceModel>();
        }
     }

还有我的控制器

var Display = new DisplaceModelInformation();
            XElement generalElement = xdoc1.Element("PlaceSearchResponse");
            Display.Dispaylist = (from c in xdoc1.Descendants("result")
                       select new DisplaceModel()
                        {
                            Name = Convert.ToString(c.Element("name").Value),
                            Address = Convert.ToString(c.Element("vicinity").Value),
                            Type = keyword,
                            Rating = (c.Element("rating") != null  ? Convert.ToString(c.Element("rating").Value) :null),
                            PhotoReference = (c.Element("photo") != null ? Convert.ToString(c.Element("photo").Element("photo_reference").Value) : null),
                            Width = (c.Element("photo") != null ? Convert.ToInt16(c.Element("photo").Element("width").Value) : 0),
                            Height = (c.Element("photo") != null ? Convert.ToInt16(c.Element("photo").Element("height").Value) : 0)
                        }).ToList<DisplaceModel>();

            return View(Display);

现在我尝试显示要查看的对象列表,我尝试了但我真的不知道如何在 mvc 视图(cshtml)文件中显示

我的 MVC 视图

@model IEnumerable<FindLocation.Models.DisplaceModelInformation>

@using (Html.BeginForm())
{
    <table>
        <tbody>
            <tr>
                <th>NAME</th>
                <th>ADDRESS</th>
                <th>RATING</th>
            </tr>

            @foreach (var element in Model)
            {
                if (element.Dispaylist.Count > 0 )
            {
                <tr>
                    <@*td>@element.Dispaylist from c </td>
                    <td>@Display.Address</td>
                    <td>@Display.Rating</td>*@
                </tr>
            }
            }
        </tbody>
    </table>

我是 UI 新手,我尝试提取对象列表,但我不知道如何列出对象。所以请帮助我....感谢您的帮助

【问题讨论】:

  • 使用@foreach (var element in Model.Dispaylist)
  • 我在它显示为错误 IEnumerable 不包含 Dispaylist 的定义之前尝试一下
  • 是的,错误是正确的。您需要将模型更改为@model FindLocation.Models.DisplaceModelInformation,因为您将Display 传递给控制器​​中的视图,该视图的类型为DisplaceModelInformation 而不是IEnumerable&lt;FindLocation.Models.DisplaceModelInformation&gt;
  • 谢谢 Yoshi 它的工作,你能解释一下为什么我们不使用 IEnumerable 我们必须使用 IEnumerable
  • 因为您只将一个DisplaceModelInformation 从控制器传递到视图。在视图中,您正在枚举DisplaceModelInformationDispaylist 属性。

标签: c# html


【解决方案1】:

部分问题可能来自您从控制器 (DisplaceModelInformation) 传递给视图的内容与视图本身中的 @model 定义 (IEnumerable&lt;DisplaceModelInformation&gt;) 之间的类型差异。更改一个以匹配另一个应该会有所帮助。

我认为您对@foreach 的想法是正确的。如果您的模型是 IEnumerable,您需要先迭代该集合,然后在另一个内部循环中迭代 DisplayList 集合。

以下问题的答案似乎有一些很好的例子来说明您要完成的工作: Foreach in a Foreach in MVC View

【讨论】:

    【解决方案2】:

    您的代码中有两个问题:

    1. 在您的 Controller 中,您需要 return View(new List&lt;DisplaceModelInformation&gt; { Display });,因为您在 View 中指定了一个 IEnumerable 模型:@model IEnumerable&lt;FindLocation.Models.DisplaceModelInformation&gt;

    2. 使用foreach 显示每个表格行:

      @foreach (var element in Model)
      {
       if (element.Dispaylist.Count > 0)
       {
           foreach (var Display in element.Dispaylist)
           {
               <tr>
                   <td>@Display.Name</td>
                   <td>@Display.Address</td>
                   <td>@Display.Rating</td>
               </tr>
           }
        }
      }
      

    【讨论】:

      猜你喜欢
      • 2016-02-21
      • 2010-12-12
      • 1970-01-01
      • 2019-11-03
      • 2021-08-14
      • 2018-10-20
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多