hsim
hsim

Reputation: 2080

Populate a dropdownlistfor with values

I'm a beginner in MVC and am trying to populate a DropDownListFor tag with values.

Here's what I found out right now which helped me a bit to construct my code:

http://forums.asp.net/t/1705047.aspx/1

So in my controller there are now 2 classes, the main class (ObjController : Controller) and another class I made up following the above example.

The other class looks like this:

public class ItemCostViewModel
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Item Cost :")]
    public string ItemCostCode { get; set; }

    public IEnumerable<ItemCostViewModel> GetItemCosts()
    {
        return new List<ItemCostViewModel>
        {
            new ItemCostViewModel() {Id = 1, ItemCostCode = "One"},
            new ItemCostViewModel() {Id = 2, ItemCostCode = "Two"},
            new ItemCostViewModel() {Id = 3, ItemCostCode = "Three"},
            new ItemCostViewModel() {Id = 4, ItemCostCode = "Four"},
            new ItemCostViewModel() {Id = 5, ItemCostCode = "Five"},
            new ItemCostViewModel() {Id = 6, ItemCostCode = "Six"}
        };
    }
}

So the goal here is to populate a DropDownListFor with values that shows "One" but from which I can refer to the id in the controller's code. My though is that if the user select "One", I get an integer value of 1 and I can treat it accordingly.

I've tried this line of code in the .cshtlm view:

@Html.DropDownListFor(m => m.ItemCostCode , new SelectList(new Project1.Controllers.ItemCostViewModel().GetItemCosts(), "Id", "ItemCostCode"))

But sadly it provokes a crash since the ItemCostCode is not located in the controller:

Compiler Error Message: CS1061:'System.Collections.Generic.IEnumerable<Project1.Models.ObjectInfo>' does not containt a definition for "ItemCostCode" (...)

I'm confused and I am not sure about what I am doing right now, and how. Could anybody help me out and explain what's wrong, and/or how to correct it?

Thanks a lot!

Upvotes: 2

Views: 325

Answers (2)

hsim
hsim

Reputation: 2080

Ok, finally, I must admit that all of you were right.

Here's what I have done which is WAY simpler than the code I was doing:

In my controller I created a dictionnary that I attributed to a ViewBag like this:

Dictionary<string, int> dictObjCost = new Dictionary<string, int>
            {
                {"One", 1},
                {"Two", 2},
                {"Three", 3},
                {"Four", 4},
                {"Five", 5},
                {"Six", 6}
            };

            ViewBag.itemCostValue = new SelectList(dictObjCost, "Value", "Key");

And in the .cshtml:

Item Cost : @Html.DropDownList("itemCostValue ", String.Empty)

So now my value shows as "One", "Two" and so on.

When I get the view, I pass the 'int? itemCostValue' parameter and everything works perfectly.

Thank you everyone to have made me realize what I was doing :)

Upvotes: 1

Corey Adler
Corey Adler

Reputation: 16137

Here's what I recommend doing to solve your problem:

1) Change the signature of that GetItemCosts() method to returning a List. You're doing it anyway, and it'll be easier in subsequent steps.

2) In your controller set the following (using whatever you do to call GetItemCosts()):

var itemCodeList = new List<DropDownItem>();

GetItemCosts().ForEach(ic => itemCodeList.Add(new DropDownItem { Value = ic.ID, Text = ic.ItemCostCode} );

ViewData["ItemCodes"] = itemCodeList;

3) The line in your View should now be able to look like the following:

@Html.DropDownListFor(m => m.ItemCostCode , ((IEnumerable<DropDownItem)(ViewData["ItemCodes"])))

Upvotes: 1

Related Questions