Maiko Kingma Maiko Kingma - 3 days ago 6
ASP.NET (C#) Question

Iterate trough List using ASP.NET Razor

I am testing if I iterate correctly trough my list of results. To do this i created a

PartialView
which creates a new
Div
with the word 'Nice'. As you can see in the image I know for sure that there are 100 results but it seems that the
Foreach
loop doesn't work. Should I iterate in a different way?

enter image description here

Webresult:

enter image description here

Code:



Controller (FeedController.cs)



public ActionResult Index()
{
return View();
}

public ActionResult _Feed()
{
return PartialView(getStatusses());
}

private List<LinqToTwitter.Status> getStatusses()
{
//Code to get tweets
}


View (Index.cshtml)



@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<div>
@{
Html.Action("_Feed");
}
</div>


PartialView (_Feed.cshtml)



@model List<LinqToTwitter.Status>

<div id="FeedPosts">
@foreach (var item in Model)
{
<div>
Nice
</div>
}
</div>

Answer

When calling Html.Action(), you need to instruct Razor to append the results of the action to the output using the @ symbol:

@Html.Action("_Feed");

Otherwise, it simply returns an MvcHtmlString that you don't use.

This applies even when the call is made inside a code block (@{...}):

@{
    @Html.Action("_Feed");
}

See MSDN

Comments