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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:02:15+00:00 2026-06-02T01:02:15+00:00

I have a function that fills creates dropdownlist in ASP.NET MVC. public static MvcHtmlString

  • 0

I have a function that fills creates dropdownlist in ASP.NET MVC.

 public static MvcHtmlString countryDropDown(this HtmlHelper helper, string name, string optionLabel, object selectedValue)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/countries.xml"));

        StringBuilder b = new StringBuilder();

        b.Append(string.Format("<select name=\"{0}\" id=\"{0}\">", name));
        if (!string.IsNullOrEmpty(optionLabel))
            b.Append(string.Format("<option value=\"\">{0}</option>", optionLabel));

        foreach (XmlNode node in doc.SelectNodes("//country"))
        {
            string selected = string.Empty;
            if (node.Attributes["name"].Value == selectedValue as string)
            {
                selected = "selected=\"selected\"";
            }
            b.Append(string.Format("<option value=\"{0}\" {2}>{1}</option>", node.Attributes["name"].Value, node.Attributes["name"].Value, selected));
        }
        b.Append("</select>");

        return MvcHtmlString.Create( b.ToString());
    }

I use this function in Create and Edit views as:

  @Html.countryDropDown("Country"," ", ViewData["Country"])

It shows list of countries perfectly but the problem is that I never selects the saved value in Edit page.
How the code can be modified so that it can select the value in edit page.

  • 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-02T01:02:17+00:00Added an answer on June 2, 2026 at 1:02 am

    Solved using

     @Html.countryDropDown("Country"," ", ViewData.Eval("Country"))
    

    instead of

     @Html.countryDropDown("Country"," ", ViewData["Country"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function in the code behind of an ASP.NET webpage that creates
I have this working function that finds folders and creates an array. function dua_get_files($path)
I have written a program with C#, that creates a logfile and fills this
I have a function that creates zip files when passed an array of files
I have this function that prints the name of all the files in a
Okay i've seen this done somewhere before where you have a function that takes
imagine you have a function that creates/copies/moves files. [logic] For the case that a
I have a function that always creates a directory and put in it some
I have a javascript function that creates an array and fill it with the
I have a function that creates 7 different text files with rows of data.

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.