.net - 在 MVC(Razor) 中用 LabelFor() 替换 DisplayNameFor() 会出现错误

标签 .net asp.net-mvc html asp.net-mvc-4 razor

问题陈述:

在 MVC 中,当我尝试在我的 Razor View (.cshtml)之一中对某些字段使用 LabelFor() 而不是 DisplayNameFor() 时,它会给出如下错误: System.Collections.Generic.IEnumaerable (Test. Models.TenantModel) 不包含 TenantID、Name、Adress1 的定义...但它与 DisplayNameFor() 一起工作正常...为什么???

而不是使用:

@Html.DisplayNameFor(model => model.TenantID)

我想使用:

@Html.LabelFor(model => model.TenantID)

因为我将显示名称作为来自数据库的 viewbag 对象的 LabelFor() 的第二个参数传递。

请给我一些建议?

查看代码:

   @model IEnumerable<Test.Models.TenantModel>

    @{
        ViewBag.Title = "Index";
    }
    <h2>Tenant</h2>


    <p>
        @Html.ActionLink("Create New", "Create")
    </p>

    <table style="border-collapse: separate; border-spacing: 15px;">
        <tr>
            @foreach (var item in ViewBag.TenantControl)
            {
                for (int i = 0; i < ViewBag.TenantLanguage.Count; i++)
                {

                    if (item == "1")
                    {
                        if (i == 0)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.TenantID)
                        </th>
                            break;
                        }
                    }

                    else if (item == "2")
                    {
                        if (i == 1)
                        {
                            <th style="text-align: center;">
                                @Html.DisplayNameFor(model => model.Name)
                            </th>
                            break;
                        }
                    }


                    else if (item == "3")
                    {
                        if (i == 2)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.Address1)
                        </th>
                            break;
                        }
                    }

                    else if (item == "4")
                    {
                        if (i == 3)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.Address2)
                        </th>
                            break;
                        }
                    }

                    else if (item == "5")
                    {
                        if (i == 4)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.Address3)
                        </th>
                            break;
                        }
                    }
                    else if (item == "6")
                    {
                        if (i == 5)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.CountryID)
                        </th>
                            break;
                        }
                    }

                    else if (item == "7")
                    {
                        if (i == 6)
                        {
                            <th style="text-align: center;">
                                @Html.DisplayNameFor(model => model.StateID)
                            </th>
                            break;
                        }
                    }

                    else if (item == "8")
                    {
                        if (i == 7)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.CityID)
                        </th>
                            break;
                        }
                    }

                    else if (item == "9")
                    {
                        if (i == 8)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.Pin)
                        </th>
                            break;
                        }
                    }
                    else if (item == "10")
                    {
                        if (i == 9)
                        {
                        <th style="text-align: center;">
                            @Html.DisplayNameFor(model => model.Phone)
                        </th>
                            break;
                        }

                    }
                }
            }
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                @foreach (var itm in ViewBag.TenantControl)
                {
                    if (itm == "1")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.TenantID)
                    </td>
                    }
                    else if (itm == "2")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.Name)
                    </td>
                    }
                    else if (itm == "3")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.Address1)
                    </td>
                    }
                    else if (itm == "4")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.Address2)
                    </td>
                    }
                    else if (itm == "5")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.Address3)
                    </td>
                    }
                    else if (itm == "6")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.CountryModel.Name)
                    </td>
                    }
                    else if (itm == "7")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.StateModel.Name)
                    </td>
                    }
                    else if (itm == "8")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.CityModel.Name)
                    </td>
                    }
                    else if (itm == "9")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.Pin)
                    </td>
                    }
                    else if (itm == "10")
                    {
                    <td style="text-align: center;">
                        @Html.DisplayFor(modelItem => item.Phone)
                    </td>
                    }
                }
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.TenantID }) |
                @Html.ActionLink("Details", "Details", new { id = item.TenantID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.TenantID })
                </td>
            </tr>
        }

    </table>

Controller 代码:

 public ActionResult Index()
    {
        var result = GetTenantData();
        return View(result);
    }



public IEnumerable<TenantModel> GetTenantData()
        {
            return (from t in db.Tenant.AsEnumerable()
                    join c in db.Country.AsEnumerable() on t.CountryID equals c.CountryID
                    join s in db.State.AsEnumerable() on t.StateID equals s.StateID
                    join ct in db.City.AsEnumerable() on t.CityID equals ct.CityID
                    orderby t.Name
                    select new TenantModel()
                    {
                        TenantID = t.TenantID,
                        Name = t.Name,
                        Address1 = t.Address1,
                        Address2 = t.Address2,
                        Address3 = t.Address3,
                        CountryID = t.CountryID,
                        StateID = t.StateID,
                        CityID = t.CityID,
                        CountryModel = c,
                        StateModel = s,
                        CityModel = ct,
                        Pin = t.Pin,
                        Phone = t.Phone,

                    });
        }

最佳答案

我真的不知道这两个函数有什么区别,但是当你调用 DisplayNameFor(model => ...) 时,模型的类型是 Test.Models.TenantModel,而使用 LabelFor(model => . ..),它是 IEnumerable (这对我来说更有意义,因为这就是您在 View 中声明的内容)。
如果你确实想让它在你的 View 中工作,你可以使用

Html.LabelFor(model => model.FirstOrDefault().TenantID)

但如果您的 Viewbag 中有空列表,我不确定它是否有效。

关于.net - 在 MVC(Razor) 中用 LabelFor() 替换 DisplayNameFor() 会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22378310/

相关文章:

c# - 如何检查 XmlDocument 是否已更改?

c# - 在没有 .NET 框架的情况下运行 .NET 应用程序

.net - 现在和将来的.Net HTML解析器

c# - MVC4 与 Telerik 编辑器无法正常工作

asp.net-mvc - 在 ASP.NET MVC 项目 : compile OK, 中重命名命名空间运行不正常

c# - Azure "500 - The request timed out"自定义错误页面

javascript - Bootstrap 容器流体 div 的右侧超出页面包装器

php - 表单提交不起作用

html - 从多个音轨视频更改音频

.net - 限制 Windows .NET 服务的 CPU 使用率