Rob
Rob

Reputation: 111

MVC5 Populate dropdownlist from a different table

My controller is as follows

 public ActionResult Create()
    {
        ViewBag.ClubList = GetClubs();

        return View();
    }

    public static List<SelectListItem> GetClubs()
    {
        List<SelectListItem> ls = new List<SelectListItem>();
        var club = new ClubsService().RecordView().ToList();
        foreach (var item in club)
        {
            ls.Add(new SelectListItem { Text = item.clubID.Trim(), Value = item.clubID.Trim() });
        }

        return ls;
    }

And my View is as follows

  @Html.DropDownListFor(model => model.clubID, ViewBag.ClubList, new { @style = "width:400px; text-align: left", @class = "btn btn-default dropdown-toggle" })

This is generating an error

('HtmlHelper' does not contain a definition for 'DropDownListFor' and the best extension method overload 'SelectExtensions.DropDownListFor(HtmlHelper, Expression>, IEnumerable, object)' has some invalid arguments.

ClubID comes from the Clubs table where the model I am populating comes from the products table.

Can someone send me in the right direction. Bit new to MVC.

Thanks.

Upvotes: 1

Views: 2209

Answers (3)

Duly Kinsky
Duly Kinsky

Reputation: 996

Instead of using the ViewBag, why not create a ViewModel for your view add a property to your ViewModel that is a selectList

public string ClubID {get; set;}
public SelectList ClubList { get; set; }

You can add all the fields your view is going to use in this model. Make sure you initialize the SelectList in the ViewModel constructor

ClubList = new SelectList();

Then inside your controller, create an instance of the view model, get the data and pass it to the view:

public ActionResult Create()
{
    var model = new MyViewModel();
    model.ClubList = GetClubs();

    return View(model);
}

public static SelectList GetClubs()
{

    var club = new ClubsService().RecordView().ToList();
    var ls = new SelectList(club, "clubID", "clubID");
    return ls;
}

And in you view, at the top, you can say:

@model namespace.Where.ViewModel

then you can say:

@Html.DropDownListFor(model => model.clubID, Model.ClubList, "Please Select...", new { @style = "width:400px; text-align: left", @class = "btn btn-default dropdown-toggle" })

Upvotes: 1

Usman
Usman

Reputation: 4703

i use another way to prevent loop and get expected result

var club = new ClubsService().RecordView().ToList();
ViewBag.ClubList = new SelectList(club, "clubID", "clubID");

first clubID defines the value and second clubID defines the text, i used both clubID Because in your example you used item.clubID and in view

  @Html.DropDownListFor(model => model.clubID, (SelectList)ViewBag.ClubList,"-- Select Clubs --", new { @style = "width:400px; text-align: left", @class = "btn btn-default dropdown-toggle" })

Upvotes: 2

Matt Spinks
Matt Spinks

Reputation: 6698

Your view file doesn't know about public static List<SelectListItem> GetClubs(). Add your namespace to the web.config file in the root of your view folders:

<pages>
    <namespaces>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Web.WebPages"/>
        <add namespace="System.Web.Helpers"/>
        <!--ADD YOUR NAMESPACE HERE-->
        <add namespace="MyCustomHelpers"/>
    </namespaces>
</pages>

Upvotes: 0

Related Questions