Link to home
Start Free TrialLog in
Avatar of Intelli-Seeker
Intelli-SeekerFlag for United States of America

asked on

I want to assign a name to an integer in MVC 5 view - to possibly put it more clearly use a word instead of number in the view

I am using Entity Framework 6 in an MVC 5 project with a SQL database. One of the tables in the database uses integer values for the status_id. I want to assign those values a name in the view rather than presenting the integers. I am having difficulty determining the best way to handle this. My first thought was using a foreach loop but I am having difficulty with the syntax and that may not be the best route.

Relevant part of the View to this question (code remove for brevity)

@model Models.Meeting

@{

    ViewBag.Title = Model.Customer1.Name;
}

<body>
    <div id = "MeetingDetails" class="EmailDetails">
        <h2>Meeting Details</h2>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Customer1.Name)
            </dt>
            <dd>
                @Html.DisplayFor(model => model.Customer1.Name)
            </dd>
            <dt>
                @Html.DisplayNameFor(model => model.Customer1.status_id)
            </dt>
            <dd>
                @Html.DisplayFor(model => model.Customer1.status_id)
            </dd>
            <br />
        </dl>
    </div>
</body>

Open in new window


Relevant portion of Controller (code removed for brevity)
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Meeting meeting = db.Meetings.Find(id);
            if (meeting == null)
            {
                return HttpNotFound();
            }
            foreach (string download in Request.Files)
            {
                if (Request.Files[download].FileName != "")
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory + "/uploads/";
                    string filename = Path.GetFileName(Request.Files[download].FileName);
                    Request.Files[download].SaveAs(Path.Combine(path, filename));
                }
            }
            return View(meeting);
        }

Open in new window


Model Diagram
User generated image
Partial screenshot of Details View. The value in the Prospect Status row is the integer value in the status_id in the Customer table.

User generated image
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

The "best" way would probably be the have a lookup table in the database and make the status_id in Customer a foreign key to it.  Then have the query return the textual name.

Not familiar with the MVC Framework but a case statement in the database call would do it:
case status_id when 0 then 'Open' when 1 then 'Closed' else 'No idea' end

Should be able to do something similar in .Net.  The way I'm familiar with in .Net is switch:
https://www.c-sharpcorner.com/article/c-sharp-switch-statement/

Never done anything with MVC.
Having a table for these status ids is typically the cleanest approach, can be maintained without code changes, etc.

But if these are very static and you want a lightweight approach, you could just add a method to you model that uses a switch statement or the like and takes in the numeric status id and returns the text description of it.  Then in your view apply that method to the status_id when you want the text version.  Or store both properties (numeric and text versions) in the model and reference the one you want.


»bp
Hi G,

If you have a few number of such options and they are going to be unique throughout, ideally you should use Enum https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum

And when you use Enum, you get the benefit of using this Helper extension : https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.html.selectextensions.enumdropdownlistfor?view=aspnet-mvc-5.2

You can also use Data Annotation to provide more meaningful names to the Enum members.

Regards,
Chinmay.
In Meeting Class include a not mapped property that return Status_Id_String that return Status name:

[NotMapped]
public string status_id_string 

     Switch (status_id)
         Case 1:
             Return "Status One";
.....

Open in new window


In page:

model => model.Customer1.status_id_string

Open in new window

Avatar of Intelli-Seeker

ASKER

Thanks for your suggestion on using the [NotMapped] attribute. However, if I use that option, it will not save to the database correct? I could use the NotMapped in the details view to show a name instead of a number, but in the create and edit views I would have to do it differently. This would be easier if I wasn't working with a system where they need to use the database in an application that will be phased out.
You can use a combobox for edit and create views it allows you to link the value of the combo box to the status_id database field and the combobox label to the status_string_id.
An alternative is to create the table STATUS_CODE with 2 columns status_id (PK) and status_name (you will have to generate the views to maintain this table). Then modify the column of the tables that use status_id as foreign key of STATUS_CODE.
@Intelli-Seeker,

Are you all set with this now, or do you need more help?  If all set, could you please close it out now.  If you need help with the question close process take a look at:



»bp
Hi Bill,

I am still working on the issue. I got sidetracked on an system issue that I had to resolve. I wear multiple hats.
Okay, that's fine, happens to all of us, just wanted to check in and see how else I/we could help.


»bp
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.