0
votes

I have a view with strongly typed model view and i need to pass this data to a partial view, in loop. (the partial view is sorta playing role of template for data)

Model:

public class Foo {

public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }

}

Controller:

public ActionResult Index()
    {
        var foo = new List<Foo>();

        for (var i = 1; i < 3; i++)
        {
            foo.Add(new Foo{Content = "test content " + i, Title = "test title " + i, Id = i});
        }
        return View(foo);
    }

View (Index):

@model List<Project.Models.Foo>
 @foreach (var foo in Model)
    {
        Html.RenderPartial("OneFoo", foo);
    }

View (OneFoo):

@using Project.Models
<div>
    Title:
        @Html.LabelFor(f => f.Title)
        Content:
        @Html.LabelFor(f => f.Content)
    }
</div>

The output I'm getting is: Title: Title, Content: Content - it's not getting the actual values.

2

2 Answers

1
votes

You are using the wrong helper, use:

@Html.DisplayFor(f => f.Content)

or

@Model.Content
1
votes
@Html.LabelFor(f => f.Title)

This does exactly what it states, prints out the Label. As Eric stated you need to use the DisplayFor to show the actual content.