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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:29:13+00:00 2026-05-25T03:29:13+00:00

I am building a website using ASP.NET MVC and entity framework. I have a

  • 0

I am building a website using ASP.NET MVC and entity framework.
I have a DB table with Country info:

CountryID int;
ShortName nvarchar;
CountryCode nvarchar;

In my View I have:

<div class="editor-field"><%: Html.DropDownListFor(model => model.address.CountryID, Model.countriesList, "<--Select Country-->") %></div>
<div class="display-field"><%: Html.TextBoxFor(m => m.phone.CountryCode)%>></div>

The list I pass onto the view is:

countriesList = new SelectList(countries, "CountryID", "ShortName");

Update The javascript code:

<script src="/Scripts/jquery-1.4.1.min.js" language="javascript" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function()
{
    $("#address.CountryID").change(function()
    {
        $.get("/PersonalInfo/GetCountryCode", $(this).val(), function(data)
        {
            $("#phone.CountryCode").val(data);
        });
    });
});

And the controller method:

public ActionResult GetCountryCode(int id)
    { 
        Country c = personalInfoRepository.GetUserCountryByID(id);
        string countryCode = c.PhoneCode;
        return Content(countryCode);
    }
</script>

What I want to do is to populate the display field with the corresponding CountryCode every time the user selects a different country from the dropdown. I understand ajax can be used to achieve this but unfortunatelly I have no experience with it. I would appreciate any help with the code or some links to easy enough tutorials for beginners.

Would it be possible to do it without posting back to the server if I passed a countryCodeList along the countriesList?

  • 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-25T03:29:14+00:00Added an answer on May 25, 2026 at 3:29 am

    Here is a great tutorial with 3 different methods for creating cascading dropdowns with ASP.NET MVC – http://stephenwalther.com/blog/archive/2008/09/07/asp-net-mvc-tip-41-creating-cascading-dropdown-lists-with-ajax.aspx

    The concept for populating a textbox will be similar. First of all you will need a controller action that will return the correct country code for a given country:

    public ActionResult GetCountryCode(int countryId)
    {
        //logic to find country code goes here
        string countryCode = "1";
        return Content(countryCode);
    }
    

    Now you will need some javascript to bind to the change event of your country dropdown and call your controller action via ajax. I recommend using jquery:

    <script type="text/javascript">
        $(document).ready(function()
        {
            $("#address_CountryID").change(function()
            {
                $.get("/Countries/GetCountryCode", $(this).val(), function(data)
                {
                    $("#phone_CountryCode").val(data);
                });
            });
        });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am building a website using asp.net mvc and jquery. as a best practice
I'm building my first ASP.NET MVC 3 app and using jqGrid. One of my
For my first ASP.NET MVC 3 application, I'm using the aspnet_Users and aspnet_Roles tables
If you at this website: http://www.illyriad.co.uk/Codex it is powered by ASP.NET MVC Now I
I've been struggling to automate the deployment of my asp.net mvc app. I found
I'm building a very simple ASP.NET user control to display messages with the help
I have inherited a large existing website to maintain. Previous developers have taken an
I'm building a website where users can view eachother's profile. When a person clicks
We are building a custom eCommerce site (for eGov functions like licenses, permits, pay

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.