【问题标题】:Change displayed name for column ASP.NET Core MVC更改列 ASP.NET Core MVC 的显示名称
【发布时间】:2021-09-16 08:40:08
【问题描述】:

我想要做的是根据数据库中的值 1=Open 和 0=Closed 在视图中显示值“Closed”或“Open”。我在 ViewModel 中尝试过这种方式:

    public int Closed 
    {
        get
        {
            string x = ReturnString;
            int y = Int32.Parse(x);
            return y;
        }
        set
        { }
    }

    public string ReturnString
    {
        get
        {
            if (Closed.ToString().Equals("1"))
            {
                return "Closed";
            }
            else (Closed.ToString().Equals("0"))
            {
                return "Open";
            }
        }
        set { }    
    }

这是我的观点:

<tbody>
    @foreach (var item in Model.Bdgfixmonths)
    {
        <td>
            @Html.DisplayFor(modelItem => item.Closed)
        </td>
    }
</tbody>

我必须在控制器中做什么才能显示值 Open/Closed 而不是 0/1?
另外,如何访问表中 ViewModel 的变量而不是模型中的变量?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc


    【解决方案1】:

    你为什么不简单地使用 ENUM:

    型号:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Reflection;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace WebAppTest1.Pages
    {
        public class DoorModel : PageModel
        {
            public List<Door> doors;
            public void OnGet()
            {
                doors = new List<Door>();
                doors.Add(new Door { DoorID = 1, DoorStatus = DoorStatuses.Open });
                doors.Add(new Door { DoorID = 2, DoorStatus = DoorStatuses.Closed });
                doors.Add(new Door { DoorID = 3, DoorStatus = DoorStatuses.Open });
                doors.Add(new Door { DoorID = 4, DoorStatus = DoorStatuses.Closed });
            }
        }
    
        public class Door
        {
            public int DoorID { get; set; }
            public DoorStatuses DoorStatus { get; set; }
        }
    
        public enum DoorStatuses
        {
            [Display(Name = "Open")]
            Open = 1,
            [Display(Name = "Closed")]
            Closed = 0
        }
    
        //Utility function to show ENUM display value
        public static class EnumExtensions
        {
            public static string GetDisplayName(this Enum enumValue)
            {
                return enumValue.GetType()
                                .GetMember(enumValue.ToString())
                                .First()
                                .GetCustomAttribute<DisplayAttribute>()
                                .GetName();
            }
        }
    }
    

    查看:

    @page
    @model WebAppTest1.Pages.DoorModel
    @{
        ViewData["Title"] = "Door";
    }
    
    <div class="text-center">
        <h1 class="display-4">Demo to show ENUM display name</h1>
    <div">
        <div class="row">
            <div class="col-4">Door ID</div>
            <div class="col-4">Status ID</div>
            <div class="col-4">Status</div>
        </div>
    
        @foreach (var door in Model.doors)
        {
            <br />
            <div class="row">
                <div class="col-4">@door.DoorID</div>
                <div class="col-4">@door.DoorStatus</div>
                <div class="col-4">@door.DoorStatus.GetDisplayName()</div>
            </div>
        }
    </div>
    </div>
    

    【讨论】:

    • 谢谢,似乎是一个非常出色的解决方案。现在的问题是,如果我把你的代码放在模型中,当我运行项目时它会抛出一个异常:“SqlException: Invalid column name 'ClosedStatus'。”,如果我把它放在 ViewModel 中,视图中会说:“ “Bdgfixmonth”不包含“ClosedStatus”的定义,并且找不到接受“Bdgfixmonth”类型的第一个参数的可访问扩展方法“ClosedStatus”(您是否缺少 using 指令或程序集引用?)。”
    • 检查最新代码,我刚刚从 Visual Studio 上的工作示例中复制。您需要根据需要修改代码,我需要根据列名更改类名、属性。首先在新项目中尝试此代码,而不是您现有的解决方案,然后您将了解在哪里更改代码。您需要以下命名空间:
    • 您需要添加以下命名空间: using System.ComponentModel.DataAnnotations;使用 System.Reflection;
    • 在将数据库值 (0, 1) 分配给 ENUM 类型属性之前,您需要转换如下值:doors.Add(new Door { DoorID = 4, DoorStatus = (DoorStatuses)0 }) ;
    【解决方案2】:

    使用如果

    <tbody>
        @foreach (var item in Model.Bdgfixmonths)
        {
           if(item.Closed==1){
               <td>
                   ...
               </td>
           }
        }
    </tbody>
    

    【讨论】:

    • 简单有效。谢谢!
    • @chdev 欢迎您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多