gpt4 book ai didi

c# - 我如何让我的观点识别我的对象

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:27 25 4
gpt4 key购买 nike

我已经创建了一个 View ,我想用它来显示从数据库中提取并在我的操作中传递给 View 的结果。当我创建 View 时,我没有告诉它我想传入哪些对象,所以我尝试手动添加它们,但 View 似乎无法识别它们。

这是我的结果 View ,顶行是我要添加的内容,但 VS 无法识别它。所以空格是红色的(无法识别),item.Name 和 item.description 是红色的

@spaces IQueryable<GiftExchange.Core.Models.Space.YogaSpace>

@{
ViewBag.Title = "Results";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h3>Search Results</h3>
@using (Html.BeginForm("Results", "Home", FormMethod.Get))
{
<p>
@Html.TextBox("Location") <input value='Search' type="submit" />
</p>
}


<table border="1">
<tr>
<th>
@Html.DisplayNameFor(spaces => spaces.Name)
</th>
<th>
@Html.DisplayNameFor(spaces => spaces.Description)
</th>
</tr>



@foreach (var item in spaces)
{
<tr>
<th>
@Html.DisplayFor(modelItem => item.Name)
</th>
<th>
@Html.DisplayFor(modelItem => item.Description)
</th>
</tr>
}

</table>

这是我的 Action

[AllowAnonymous]
public ActionResult Results(string searchTerm)
{
IQueryable<YogaSpace> spaces;
using (var repo = new YogaSpaceRepository())
{
spaces = repo.All;
}

return View(spaces);
}

最佳答案

在 View 的第一行,将 @spaces 切换为 @model

然后在您想使用它的地方,使用 Model 而不是 spaces

有关更多详细信息,请在谷歌上搜索“强类型 View ”。

关于c# - 我如何让我的观点识别我的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28097605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com