1

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>
2
  • It should be working as expected. What issue you are having ? Commented Nov 29, 2016 at 11:20
  • @haim770 thanks for your quick answer. I didn't realise there was a difference between @ { Html.Action("_Feed") } and `@Html.Action("_Feed") Commented Nov 29, 2016 at 11:25

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

The surrounding @{ } is not really necessary
@StephenMuecke, It's only there to demonstrate than even though you already in a @{...} block, you still need the other @.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.