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 8753249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:23:27+00:00 2026-06-13T13:23:27+00:00

I have been banging my head for 2 days now with Html.DropDownListFor and MVC3.

  • 0

I have been banging my head for 2 days now with Html.DropDownListFor and MVC3.

I have been successful in creating a drop down list and successfully updating the database
based on it.

I create a drop down list for area

1 - NewYork
2 - London
3 - Paris
etc

The problem occurs when creating a edit page where the user can change the previous
choice. I am trying to show the previous selected value in the drop down.

My code is as follows:

Controller:

//
// GET: /MyHome/UpdateProfile

public ActionResult UpdateProfile()
{
    var userProfile = websiteRepository.GetProfileForLoggedUser();
    UpdateProfileViewModel viewModel = new UpdateProfileViewModel
    {
       AreaLookUps = websiteRepository.GetAreaLookUpSelectList(userProfile.Area),
       UserProfile = userProfile
    };
    return View("UpdateProfile", viewModel);
}

The GetAreaLookUpSelectList method

public IEnumerable<SelectListItem> GetAreaLookUpSelectList(int selectedId)
{
   var areaLookUps = from p in db.AreaLookUp
                     orderby p.AreaLookUpDescription
                     select new SelectListItem
                     {
                        Text = p.AreaLookUpDescription,
                        Value = SqlFunctions.StringConvert((double)p.AreaLookUpId),
                        Selected = p.AreaLookUpId == selectedId
                     };
    return areaLookUps;
}

View

@model NPLHWebsite.ViewModels.UpdateProfileViewModel

<div class="editor-label">
   @Html.LabelFor(model => model.UserProfile.Area)
   @Html.DropDownListFor(model => model.UserProfile.Area, Model.AreaLookUps, "--Select Area--")
</div>

ViewModel

public class UpdateProfileViewModel
{
   public UserProfile UserProfile { get; set; }
   public IEnumerable<SelectListItem> AreaLookUps { get; set; }
}

Please help me display a selected value. Say “London” in the dropdownlist, if that option was selected while creating the profile. The database stores the value of 2 to represent London.

  • 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-13T13:23:28+00:00Added an answer on June 13, 2026 at 1:23 pm

    It looks like the AreaLookUpId property is a double which is causing issues when comparing with the selected value because you are using SqlFunctions.StringConvert and the format might not match exactly.

    I would recommend you using integers or guids for id properties. Also you can remove the Selected = p.AreaLookUpId == selectedId from your GetAreaLookUpSelectList function. It is enough to set the viewModel.UserProfile.Area property in your controller action to the corresponding value (which you have already done):

    public IEnumerable<SelectListItem> GetAreaLookUpSelectList(int selectedId)
    {
        return db
            .AreaLookUp
            .OrderBy(x => x.AreaLookUpDescription)
            .ToList()
            .Select(x => new SelectListItem 
            {
                Value = x.AreaLookUpId.ToString(),
                Text = x.AreaLookUpDescription
            });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been banging my head over for two days now. I have a
I have been banging my head on the desk for several days over this
I have been banging my head of the wall for two days with this
I have been banging my head against this for a little while now. I
I have been banging my head against this for some time now: I want
I have been banging my head over this one for quite a while now
been banging my head for 2 days now I am a newbie to zend
I have been banging my head against this problem for days, and searched exhaustively
I have been banging my head against this one for a while now and
I have been banging my head for a while now, understanding java language. I

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.