0

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.

4

2 回答 2

1

您使用了错误的助手,请使用:

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

或者

@Model.Content
于 2012-12-20T05:11:42.593 回答
1
@Html.LabelFor(f => f.Title)

这正是它所说的,打印出标签。正如 Eric 所说,您需要使用 DisplayFor 来显示实际内容。

于 2012-12-20T05:14:08.057 回答