Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code is given below

code
----
var usrModel = new UserRegisterModel();
usrModel.Countries = snwe.Countries..AsEnumerable().Select(C => new SelectListItem
{
Text = C.Country1,
Value = C.CountryID.ToString()
});

view.cshtml
------------

  • <label>@(LocalizationProvider.Current.CountryLabel)*
    @Html.ValidationMessageFor(m => m.CountryId)</label>

    @Html.DropDownListFor(m => m.CountryId, Model.Countries, "Select Country", new { @class = "dropdown" })


  • In the above code the code the countries are listing but i need to in dropdown first value is "india".
    Posted

    You can create a separate method to bind country dropdown-

    C#
    public static SelectList GetCountries(int selected = 0)
           {
               return new SelectList(Repository.Instance.All<country>(), "Id", "Name", selected);
           }</country>

    C#
    @Html.DropDownListFor(x=>x.CountryId,GetCountries(x.CountryId),"[Select]")


    The method return type is SelectList so you just need to pass the Id of India in GetCountries method as parameter.

    another way you can add all the items into list except India and then insert India at position 0 or add India as first row in your database table, or create a column int_Order in your database table and user OrderBy clause.
     
    Share this answer
     
    1.First you should filter out "India" from your selection by using WHERE condition in your fist select (all countries except India);

    2.Then, by using a similar select (but this time in your WHERE clause select only India), you should insert (add in the first position) "India" as first item in your "usrModel.Countries";

    3.Here is how the code should look like:
    C#
    List<Country> tempList = snwe.Countries.Where(c=>c.CountryShort != "IN").ToList();
    Country indiaEntry = snwe.Countries.Where(c => c.CountryShort == "IN").FirstOrDefault();
    //
    if(indiaEntry  != null)
       tempList.Insert(0, indiaEntry); 
    //
    usrModel.Countries = tempList.Select(C => new SelectListItem
    {
    Text = C.Country1,
    Value = C.CountryID.ToString()
    });
     
    Share this answer
     
    v2
    Comments
    Athul MS 19-Oct-15 3:34am    
    in the above way in dropdown only binding the inda

    The code is given below
    usrModel.Countries = snwe.Countries.Where(c=>c.CountryShort != "IN").AsEnumerable().Select(C => new SelectListItem
    {
    Text = C.Country1,
    Value = C.CountryID.ToString()
    });
    usrModel.Countries = snwe.Countries.Where(c => c.CountryShort == "IN").AsEnumerable().Select(C => new SelectListItem
    {
    Text = C.Country1,
    Value = C.CountryID.ToString()
    });
    Raul Iloc 19-Oct-15 4:08am    
    You should modify you code by using a temp List<"YourCountryEntity"> for storing the results from first Select, then Insert into this list the result from 2nd list (for India) and finally save the results from the list into "usrModel.Countries".
    Athul MS 19-Oct-15 4:17am    
    i need the code
    Raul Iloc 20-Oct-15 1:37am    
    Did you see my update? It should work now.
    Athul MS 19-Oct-15 4:24am    
    in jquery how it is possible
    C#
    var usrModel = new UserRegisterModel();
    usrModel.Countries = snwe.Countries..AsEnumerable().Select(C => new SelectListItem
    {
        Text = C.Country1,
        Value = C.CountryID.ToString(),
        Selected = "INDIA Country ID"
    });

    -KR
     
    Share this answer
     
    ddlFruits.Items.insert(0,"India")
     
    Share this answer
     

    This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



    CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900