Asp.net mvc 4 HTML.Display()不显示任何内容
我刚开始使用mvc,代码如下:Asp.net mvc 4 HTML.Display()不显示任何内容,asp.net-mvc-4,Asp.net Mvc 4,我刚开始使用mvc,代码如下: @model AzureDemo.Models.User @{ ViewBag.Title = "Interests"; } <h2>Interests</h2> <p> @Html.ActionLink("Logout", "Logout") </p> <table> <tr> <th> Interests
@model AzureDemo.Models.User
@{
ViewBag.Title = "Interests";
}
<h2>Interests</h2>
<p>
@Html.ActionLink("Logout", "Logout")
</p>
<table>
<tr>
<th>
Interests
</th>
</tr>
@foreach (var interest in Model.Interests) {
<tr>
<td>
@Html.Display(interest)
</td>
//Tried like this
<td>
@Html.Display("id", interest.ToString())
</td>
</tr>
}
</table>
User中的Interests属性只是一个字符串列表。我试图为用户显示表中的每个兴趣。我还尝试在Html.Display中放置一个类似字符串的测试,或者尝试使用ToString,但仍然没有任何结果。您可以直接使用类似这样的模型项
@foreach (var interest in Model.Interests) {
<tr>
<td>
@interest
</td>
// or this
<td>
@interest.ToString()
</td>
</tr>
}
或者,如果您在视图中显示html代码,则更安全
@foreach (var interest in Model.Interests) {
<tr>
<td>
@Html.Raw(interest)
</td>
</tr>
}
也谢谢你给我这个机会 您可以像这样直接使用模型项
@foreach (var interest in Model.Interests) {
<tr>
<td>
@interest
</td>
// or this
<td>
@interest.ToString()
</td>
</tr>
}
或者,如果您在视图中显示html代码,则更安全
@foreach (var interest in Model.Interests) {
<tr>
<td>
@Html.Raw(interest)
</td>
</tr>
}
也谢谢你给我这个机会 我建议您使用一个显示模板,并去掉视图中的所有foreach循环:
@model AzureDemo.Models.User
@{
ViewBag.Title = "Interests";
}
<h2>Interests</h2>
<p>
@Html.ActionLink("Logout", "Logout")
</p>
<table>
<thead>
<tr>
<th>
Interests
</th>
</tr>
</thead>
<tbody>
@Html.DisplayFor(x => x.Interests)
</tbody>
</table>
我建议您使用显示模板,并在视图中删除所有foreach循环:
@model AzureDemo.Models.User
@{
ViewBag.Title = "Interests";
}
<h2>Interests</h2>
<p>
@Html.ActionLink("Logout", "Logout")
</p>
<table>
<thead>
<tr>
<th>
Interests
</th>
</tr>
</thead>
<tbody>
@Html.DisplayFor(x => x.Interests)
</tbody>
</table>
你为什么不直接使用?像这样:@interest这很有效!如果你把它作为一个答案,我会接受:你为什么不直接使用?像这样:@interest这很有效!如果你把它作为一个答案,我会接受:谢谢我会尝试这个,但是我已经告诉AliRiza我会接受他的答案:谢谢我会尝试这个,但是我已经告诉AliRiza我会接受他的答案: