Home > Software design >  Change displayed name for column ASP.NET Core MVC
Change displayed name for column ASP.NET Core MVC

Time:09-17

What I want to do is display the value "Closed" or "Open" in the view based on the value in the database which is 1=Open and 0=Closed. I have tried this way in the 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 { }    
    }

And this is my View:

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

What I have to do in the Controller to display the value Open/Closed instead of 0/1?
Also how do I access the variable of the ViewModel in the table instead of the one in the Model?

CodePudding user response:

Why don't you simply use ENUM:

Model:

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();
        }
    }
}

View:

@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>

CodePudding user response:

Use if

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