我如何让我的观点识别我的对象
How do I get my view to recognize my object
我已经创建了一个视图,我想用它来显示从数据库中提取并在我的操作中传递给视图的结果。当我创建视图时,我没有告诉它我想传入哪些对象,所以我尝试手动添加它们,但视图似乎无法识别它们。
这是我的结果视图,顶行是我要添加的内容,但 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>
这是我的动作
[AllowAnonymous]
public ActionResult Results(string searchTerm)
{
IQueryable<YogaSpace> spaces;
using (var repo = new YogaSpaceRepository())
{
spaces = repo.All;
}
return View(spaces);
}
在您视图的第一行,将 @spaces
切换为 @model
。
然后在你想用的地方,不用spaces
,用Model
。
有关更多详细信息,google for "strongly typed views"。
我已经创建了一个视图,我想用它来显示从数据库中提取并在我的操作中传递给视图的结果。当我创建视图时,我没有告诉它我想传入哪些对象,所以我尝试手动添加它们,但视图似乎无法识别它们。
这是我的结果视图,顶行是我要添加的内容,但 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>
这是我的动作
[AllowAnonymous]
public ActionResult Results(string searchTerm)
{
IQueryable<YogaSpace> spaces;
using (var repo = new YogaSpaceRepository())
{
spaces = repo.All;
}
return View(spaces);
}
在您视图的第一行,将 @spaces
切换为 @model
。
然后在你想用的地方,不用spaces
,用Model
。
有关更多详细信息,google for "strongly typed views"。