【发布时间】: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<FindLocation.Models.DisplaceModelInformation> -
谢谢 Yoshi 它的工作,你能解释一下为什么我们不使用 IEnumerable
我们必须使用 IEnumerable -
因为您只将一个
DisplaceModelInformation从控制器传递到视图。在视图中,您正在枚举DisplaceModelInformation的Dispaylist属性。