0

I have the following data in the controller

private void GetCountryLists(string value)
    {
        List<country> countries = new List<country>();
        country _country = new country() { countryname = "Select a country", value = "0" };
        country _country1 = new country() { countryname = "India", value = "India" };
        country _country2 = new country() { countryname = "USA", value = "USA" };
        countries.Add(_country);
        countries.Add(_country1);
        countries.Add(_country2);

        ViewData["country"] = new SelectList(countries, "value", "countryname", value);
    }

The country class is as follows,

public class country
{
    public string countryname { get; set; }
    public string value { get; set; }
}

Now, how do i use the ViewData["country"] data from the view page using the Html.DropDownListFor<>() to display the country list. In the viewpage, i have the Model property from which i can get the user's country value. I have to display the user country in the dropdown list with the selection of the current user's country.

This code is for the edit page.

Kindly suggest me a simple approach so that i can learn and use as i am a newbie developer.

1 Answer 1

4

Here's one way (not recommended because of ViewData, you directly may skip to the second suggestion):

Assuming you have the following controller action:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.SelectedCountryValue = "India";
    GetCountryLists(model.SelectedCountryValue);
    return View(model);
}

and in the view:

<%= Html.DropDownListFor(
    x => x.SelectedCountryValue, 
    (SelectList)ViewData["country"]
) %>

And now the recommended way:

Model:

public class MyViewModel
{
    public string SelectedCountryValue { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }
}

Controller:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        SelectedCountryValue = "India",
        Countries = GetCountries()
    };
    return View(model);
}

private IEnumerable<SelectListItem> GetCountries()
{
    return new[]
    {
        new SelectListItem { Value = "India", Text = "India" },
        new SelectListItem { Value = "USA", Text = "USA" },
    }
}

and in the view:

<%= Html.DropDownListFor(
    x => x.SelectedCountryValue, 
    new SelectList(Model.Countries, "Value", "Text"),
    "Select a country"
) %>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your reply. By the way, how do i add checkboxes groups. Currently, i am using like this : { <% Type enumType = typeof(UserManagementSystem.Controllers.qualification);%> <% for (int i = 0; i < Enum.GetNames(enumType).Length; i++) { %> <%= Enum.GetNames(enumType)[i] %> <input type="checkbox" id="qualifications" name="qualifications" value="<%= Enum.GetNames(enumType)[i] %>" /> <br /> <%} %> } with the enum as. [i will post in next comment]
{ public enum qualification { c = 1, cpp = 2, java = 3, dotnet = 4, php = 5 } }. Also kindly suggest the ways to validate this checkbox group.Also if this way of generating the check box group is wrong, suggest me the correct procedure. thanks once again for your reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.