Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hello, please all programmers i need your help in this:
i have searched and tried every example i find on the internet but no one try to work for me.
What i am trying to do is that i want to create a dropdownlisfor in the registration form so that everyone registering as a new user can select his/her country from a dropdownlist.
Now, most articles do say dropdownlist loads items from the database but i don't understand how that is possible in the sence that you are just newly creating an application but they will never tell you how to write to the database.
Right now, the method i have for dropdownlist with item is if i include a foreignkey into another entity.
Please, i need a programmer to show me in his own understanding how my view, controller(get & post) and model will look like. Please
Posted
Updated 28-Nov-16 19:23pm

1 solution

1.Add below action method in HomeController
C#
public ActionResult BindWithModel()
{
    List<SelectListItem> items = new List<SelectListItem>();

    items.Add(new SelectListItem 
                { Text = "Select Category", Value = "0", 
                        Selected = true });
    items.Add(new SelectListItem
                { Text = "Beverages", Value = "1" });
    items.Add(new SelectListItem
                { Text = "Condiments", Value = "2" });
    items.Add(new SelectListItem
                { Text = "Confections", Value = "3" });
    items.Add(new SelectListItem
                { Text = "Dairy Products", Value = "4" });
    items.Add(new SelectListItem
                { Text = "Grains/Cereals", Value = "5" });
    items.Add(new SelectListItem
                { Text = "Meat/Poultry", Value = "6" });
    items.Add(new SelectListItem
                { Text = "Produce", Value = "7" });
    items.Add(new SelectListItem
                { Text = "Seafood", Value = "8" });

    var model = new CategoryModel()
    {
        lstCategory = items,
        selected = 1
    };

    return View(model);
}


2.Add below HTML code to View
HTML
@Html.DropDownListFor(x => x.selected, Model.lstCategory)
 
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