ASP NET MVC C# - .Model 是一个 属性 但它像一个类型一样使用

ASP NET MVC C# - .Model is a property but it used like a type

我在访问要查看的模型的 属性 时遇到困难。

  1. Error: Mvc.Webviewpage<tmodel>.Model is a property but it used like a type。在这段代码中 @foreach (Model.ShoutboxObject record in Model)

  2. 我想访问名为 ShoutboxIdModel 的 属性,但出现错误。

  3. 谁能帮我解决这个问题。我刚开始学这个。

我的模型视图中的代码:

public class ShoutboxMainViewModel
{
    public dynamic DepartmentId { get; set; }

    // Here is the property that I want to access
    public IEnumerable<ShoutboxViewModel> ShoutboxObject { get; set; }  

public class ShoutboxViewModel
{
    // I want to access this to my view and put into input type hidden.
    public string ShoutboxId { get; set; }      

    public dynamic ShoutboxTitle { get; set; }

我的 .cshtml 视图中的代码:

@model IEnumerable<WMSPortal.Models.ShoutboxMainViewModel>

    @foreach (Model.ShoutboxObject record in Model) {   *//error here*

    <text>
        <input type="hidden" class="shoutbox-id" value="@record.ShoutboxId" />  
    </text> 

您将需要 2 个循环(或使用 LINQ),因为您有一个 IEnumerable<WMSPortal.Models.ShoutboxMainViewModel> 模型,以及该模型的 IEnumerable<ShoutboxViewModel> 属性。

@foreach (var viewmodel in Model) {
    @foreach (var record in viewmodel.ShoutboxObject) {
        <text>
            <input type="hidden" class="shoutbox-id" value="@record.ShoutboxId" />  
        </text>  
    }
}