chuckd
chuckd

Reputation: 14540

How do I get my view to recognize my object

I already have a view created that I want to use to display results being pulled from the db and passed to the view in my action. When I created the view I didn't tell it what objects I wanted to pass in so I'm trying to add them manually but the view doesn't seem to recognize them.

Here is my Results view and the top line is what I'm trying to add, but VS doesn't recognize it. So spaces is red (unrecognized) and item.Name and item.description are red

@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>

Here is my action

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

        return View(spaces);
    }

Upvotes: 1

Views: 54

Answers (1)

gabsferreira
gabsferreira

Reputation: 3137

In the first line of your view, switch @spaces for @model.

And then where you want to use it, instead of spaces, use Model.

For more details, google for "strongly typed views".

Upvotes: 4

Related Questions