Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:24:06+00:00 2026-06-17T05:24:06+00:00

Maybe this is very simple to do but I can’t find the good words

  • 0

Maybe this is very simple to do but I can’t find the good words when I search on stackoverflow or on google.

I have a model and this model contains a “Country” property that is a integer. When I am in the Edit view, this property is used this way and it work well.

@Html.DropDownListFor(model => model.Country, new SelectList(ViewBag.Countries, "IDCountry", "Name"))   

In the Details view, I only want to show the name of the country, but I don’t know how! Right now, I’m doing this but it only show the ID and I can’t find a way to give him the List so it use it as a datasource or something like that to show “Canada” instead of 42.

@Html.DisplayFor(model => model.Country)

How can this be achieved?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:24:08+00:00Added an answer on June 17, 2026 at 5:24 am

    How can this be achieved?

    By using a view model of course:

    public class CountryViewModel
    {
        public int CountryId { get; set; }
        public string CountryName { get; set; }
    }
    

    and then you would populate this view model in the controller action that is supposed to render your Display view:

    public ActionResult Display(int id)
    {
        Country country = ... go and fetch from your db the corresponding country from the id
    
        // Now build a view model:
        var model = new CountryViewModel();
        model.CountryId = country.Id;
        model.CountryName = country.Name;
    
        // and pass the view model to the view for displaying purposes
        return View(model);
    }
    

    Now your view will be strongly typed to the view model of course:

    @model CountryViewModel
    @Html.DisplayFor(x => x.CountryName)
    

    So as you can see in ASP.NET MVC you should always be working with view models. Think in terms of what information you need to work with in a given view and the first thing you should do is define a view model. Then it’s the responsibility of the controller action that is serving the view to populate the view model. Where the values of this view model are coming from doesn’t really matter. Think of the view model as a single point of aggregation of many data sources.

    As far as the views are concerned, they should be as dumb as possible. Simply work with what’s available in the view model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this is a very simple question, but I can't find the answer: How
simple question maybe but can't find solution. I have button for refresh/update some contents
Maybe this is a very simple question, but I just can not figure out.
Maybe this is very basic, but I am all confused. I have a simple
Maybe a very simple question. How can I put in this code <Query> <Where>
Maybe this is a poor question, but, I can't find a tutorial or even
I think my question is very simple but I have search the stackover and
maybe this is a very simple question, but forgot the answer about it because
this might be a very simple question, but I could not find an answer
I have this strange, yet maybe very simple problem.. I have a timer1 in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.