Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good day, i want to create a dropdownlist using the htmhelper classes, i'm very new to this and it's already looking confusing.

i have a list of items that i want to add to this dropdown and that list of items are in a generic <list>
C#
List<string> Name = new List<string>();
in my model as shown

i added the items to the list using the getState() method

C#
namespace newNSMS.Areas.Ministries.Models
{
    public class Category
    {
        public List<string> Name = new List<string>();        
    }
    
    public class getMethods
    {
        public static Category getState(Category d)
        {
            try
            {
                string countCode = "Nig";
                char[] chars = { ',' };

                testStealthServ.countries csd = new testStealthServ.countries();
                List<string> sth = csd.getStates(countCode).ToList<string>();
                foreach (var v in sth)
                {
                    string[] splits = v.Split(chars);

                    d.pID += splits.ElementAt(1);
                    d.pName += splits.ElementAt(0);

                    d.Name.Add(splits.ElementAt(0));
                    d.ID.Add(splits.ElementAt(1));
                }

                return d;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
    }
}


i want the dropdownlist to be populated in my view on page load so i did this in my controller class
C#
public ActionResult MinistriesHome()
{
    Category c = new Category();
    getMethods.getState(c);

    return View(c);
}


how do i connect the list items to the htmlhelper dropdownlist as shown below

C#
<div class="col-sm-10">
    @Html.DropDownList("schlreg", (SelectList)Model => Model.Name, null, new { @class = "Category" })
</div>


i really have no idea what i'm doing in the view. the form name is "schlreg". please assist me on how to populate the view with the model list data.

Thanks
Posted

C#
@Html.DropDownList("mylist", new SelectList(Model.Name))


this worked perfectly well where Model.Name is a generic list in my model class that has an array of items that i want to pass into the dropdownlist
 
Share this answer
 
v2
You can create strongly type partial view

HTML
<div class="col-sm-10">
    @Html.DropDownList("schlreg", (SelectList)Model => Model.Name, null, new { @class = "Category" })
</div>



when you call the main view
@Html.Partial("~/Views/dropdown.cshtml", model)
 
Share this answer
 
Comments
EasyHero 18-Jan-16 8:14am    
I have errors on this line (SelectList)Model => Model.Name, null, new
Error that says something like "cannot convert Models.Category to System.Web.Mvc.SelectList"
Category is a class in my models and it contains a generic list called Name

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