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

  • SEARCH
  • Home
  • 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 7025897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:02:42+00:00 2026-05-28T00:02:42+00:00

I am trying to implement editing addresses in my view. An address has basic

  • 0

I am trying to implement editing addresses in my view.

An address has basic strings for street, town etc. It also has a country id. I have a table in my database of countries.

In my controller I create a SelectList for these countries:

ViewBag.Countries = _countryRepo.GetAll().Select(c => new SelectListItem { Text = c.Name, Value = c.Id });

Now in my view I want to be able to use this select list to allow users to select the country.

I originally thought:

@Html.DropDownList("countryId", (IEnumerable<Country>)ViewBag.Countries, "Select")

I have two addresses:

public class Company() 
{
    public Address HeadOffice { get; set; }
    public Address SecondOffice { get; set; }
}

So when displaying the view I need to have the select country shown. I don’t think I can do this in my controller because if I select one like this:

 ViewBag.Countries = _countryRepo.GetAll().Select(c => new SelectListItem { Text = c.Name, Value = c.Id, Selected = c.Id == model.HeadOffice.Id ? true : false });

This will only work for the HeadOffice.

From what I can there are two options. Have two different select lists or manually build the select list in the view:

<select name="HeadOffice.CountryId">
    @foreach(var c in (IEnumerable<Country>)ViewBag.Countries)
    {
         <option value="@c.Id" @(c.Id == Model.HeadOffice.Id ? "selected='true'" : "">@c.Name</option>
    }
</select>

What is the best way? Both seem wrong and not nice. Is there a better way to approach this problem?

  • 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-05-28T00:02:43+00:00Added an answer on May 28, 2026 at 12:02 am

    I would use a ViewModel that takes in the list of countries once then expose 2 different lists. This way you only query the database ones but still keep you View clean

    ViewModel:

    public class MyViewModel 
    {
      public Address HeadOffice { get; set; }
      public Address SecondOffice { get; set; }
    
      public IEnumerable<Country> Countries { get;set;}
    
      public IEnumerable<SelectListItem> HeadOfficeCountries
      {
         get 
         {
           return GetCountriesList(Countries, HeadOffice.Id);
         }
      }
    
      public IEnumerable<SelectListItem> SecondOfficeCountries
      {
         get 
         {
           return GetCountriesList(Countries, SecondOffice.Id);
         }
      }
    
      private IEnumerable<SelectListItem> GetCountriesList(IEnumerable<Country> countries, int forAddress)
      {
         return countries.Select(c => new SelectListItem { Text = c.Name, Value = c.Id, Selected = c.Id == forAddress ? true : false });
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a ListView data control for displaying and editing lookup
I'm currently trying to implement a editable details view using a grouped UITableView. I'd
I'm working on a project that's trying to implement some editing features using a
I'm trying to implement the WMD editor used on StackOverflow to create some basic
I'm trying to figure out how to implement this relationship in coldfusion. Also if
I am trying to implement deletable UITableViewCells. My UITableView has 3 sections. The 1st
I'm trying to implement a form that I can reuse for adding or editing
For a few weeks now, I have been trying to implement a DFT that
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all

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.