siri
siri

Reputation: 133

DropDownListFor in MVC3

OK first of all, am new to ASP.NET MVC. Now i am trying to add a dropdown list in a C# Razor view. Question is, is there anyway to directly assign List() to the @Html.DropDownListFor...? MyClass goes like this

public class MyClass
{
  public int ID {get; set;}
  public string Itemstring {get; set;}
}

ViewModel goes like this

public class MyViewModel
{
  public int ID { get; set; }
  public List<MyClass> MyClassList {get; set; }
}

Controller action method goes like this

public ActionResult Home()
{
MyViewModel mvm = new MyViewModel () {
new MyClass() { ID=1, Itemstring="My Item1" },
new MyClass() { ID=2, Itemstring="My Item2" },
new MyClass() { ID=3, Itemstring="My Item3" }
}
}

On View: THIS IS WHERE I AM NOT SURE HOW TO USE

@model xyzNameSpace.Models.MyViewModel
<div>
@Html.DropDownListFor(x => x.ID, Model.MyClassList);
<a href="@Url.Action("Selected")><img src="../../Content/images/selected.gif" alt="Proceed" /> </a>
</div>

-- So when user selects one item from dropdown and clicks on image, it should invoke (or call not sure which word to use) 'Selected' action in the control and it should bind the model with selected value, Could some one help me how to do this...?

Thank you Siri.

Upvotes: 3

Views: 9006

Answers (3)

MaTya
MaTya

Reputation: 782

in model

    public class modelname
            {
              public selectlist getdropdown()
               {
                  IEnumerable<SelectListItem> EntityList = edmx.tblEntities.AsEnumerable().Select(a => new SelectListItem { Text = a.name, Value = a.id.ToString() }).ToList();
                   return new SelectList(EntityList, "Value", "Text");             

 }
        }

in view

@Html.DropDownListFor(model => model.source_entity, Model.getdropdown(), "--Select--")

in contoller

public ActionResult home()
        {
            return View(new modelname());
        }

Upvotes: 1

Dmitry Efimenko
Dmitry Efimenko

Reputation: 11203

Html.DropDownListFor has 6 overloads and all of them take IEnumerable<SelectListItem> as the parameter that will hold your values. You cannot dirrecly use your own class for that, which is why you get that error when you tried solution by Kyle.

Add this to your model:

public List<SelectListItem> ListToUse { get; set; }

Controller:

MyViewModel model = new MyViewModel();
model.MyClassList = new List<MyClass>
{
    new MyClass() { ID=1, Itemstring="My Item1" },
    new MyClass() { ID=2, Itemstring="My Item2" },
    new MyClass() { ID=3, Itemstring="My Item3" }
}
model.ListToUse = model.MyClassList
    .Select(x => new SelectListItem{ Value = x.ID, Text = x.Itemstring })
    .ToList();

In the view:

@Html.DropDownListFor(x => x.ID, Model.ListToUse);

I did not test this code.

Upvotes: 5

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

If I understand you correctly, you want to post the selected item from the dropdownlist to the server when the "Proceed" button is clicked?

You will need to use a form to do this:

@model xyzNameSpace.Models.MyViewModel
@using(Html.BeginForm())
{
    <div>
    @Html.DropDownListFor(x => x.ID, Model.MyClassList);

    <input type="submit" value="Proceed" />
    </div>
}

Upvotes: 1

Related Questions