ViewData不在View中显示内容(asp.net mvc c#)

时间:2022-07-17 03:44:06

I'm using ASP.NET MVC4 C# and I'm facing a problem with displaying an array in my View:

我正在使用ASP.NET MVC4 C#,我在我的视图中显示一个数组时遇到问题:

I'd like to display a number of error messages from my controller to the view, but so far, the content of only 1 array is displayed...

我想从控制器向视图显示一些错误消息,但到目前为止,只显示了1个数组的内容...

Only the data in "ViewData["mededelingen"] is displayed. The data in ViewData["warnings"] is not displayed, despite the same code.

仅显示“ViewData [”mededelingen“]中的数据。尽管代码相同,但不显示ViewData [”warnings“]中的数据。

Below is the code of the view:

以下是视图的代码:

ViewData不在View中显示内容(asp.net mvc c#)

When I debugged, I have noticed that the ViewData["warnings"] is not empty, as shown in the next screenshot:

当我调试时,我注意到ViewData [“warnings”]不为空,如下一个屏幕截图所示:

ViewData不在View中显示内容(asp.net mvc c#)

However, it does not display the contents on my HTML page.

但是,它不会在我的HTML页面上显示内容。

Below is the output:

以下是输出:

ViewData不在View中显示内容(asp.net mvc c#)

As you can see, only the items in red (mededelingen) are displayed, the items in yellow (warnings) are not.

如您所见,仅显示红色(mededelingen)中的项目,黄色(警告)中的项目不显示。

Below is the code of the controller:

以下是控制器的代码:

ViewData不在View中显示内容(asp.net mvc c#)

Obviously I'm doing something wrong, but I can't figure out what exactly... Any help?

显然我做错了什么,但我无法弄清楚究竟是什么......有什么帮助吗?

Thanks in advance!!

提前致谢!!

1 个解决方案

#1


1  

DisplayName gets the display attribute of the model property represented by the string that's passed to it. Since your string is just a sentence, that doesn't make sense. Why are you using DisplayName at all?

DisplayName获取由传递给它的字符串表示的model属性的display属性。因为你的字符串只是一个句子,所以没有意义。你为什么要使用DisplayName?

Just do:

做就是了:

@foreach (var counter2 in (ViewData["warnings"] as List<string>))
{
    <td>@counter2</td>
}

#1


1  

DisplayName gets the display attribute of the model property represented by the string that's passed to it. Since your string is just a sentence, that doesn't make sense. Why are you using DisplayName at all?

DisplayName获取由传递给它的字符串表示的model属性的display属性。因为你的字符串只是一个句子,所以没有意义。你为什么要使用DisplayName?

Just do:

做就是了:

@foreach (var counter2 in (ViewData["warnings"] as List<string>))
{
    <td>@counter2</td>
}