0

I am trying to create a dropdown list to display all the value in a custom collection class such as

public class MyCustomClassCollection
{

public List<MyCustomClass> {get;set;}

}

I want it to show the Description:string of each MyCustomClass

I tried

<%: Html.DropDownList "Description", MyCustomClass %>

Resharper suggests that I cast MyCustomClass to IEnemerable

but the server returns an unable to cast error.

Any Idea how I can create this DropDownList?

__Modification___

public class ViewModel
    {
        public Detail detail { get; set; }
    }

public class Detail   //Inherited from webservce
{
   public CustomClassCollection {get;set;}
   .... Other Properties, a.k.a Custom Classes
}


public class CustomClassCollection
{
   public List<CustomClass> {get;set;}
}

public class CustomClass {
  public int Id {get;set;}
  public string Description{get;set;}
  ... other properties

}


public ActionResult Index(int? id, DateTime? date)
        {
            if (id.Equals(null))
                id = ######### ;
            if (date.Equals(null))
                date = DateTime.Today;
            var vm = new ViewModel
                         {
                             Detail = _repository.Detail((int)id,(DateTime)date)
                         };
            return View(vm);
        }

1 Answer 1

3

The second argument of the DropDownList helper must be an IEnumerable<SelectListItem> or a SelectList which implements this interface for that matter. So in your controller action organize in such a way that you convert your custom collection into an IEnumerable<SelectListItem>. As always you could start by writing a view model:

public class MyViewModel
{
    public string SelectedDescription { get; set; }
    public SelectList Descriptions { get; set; }
}

and then have your controller action query the custom list and populate the view model which will be passed to the view:

public ActionResult Index()
{
    var descriptions = yourCustomCollection.MyCustomClass.Select(x => new
    {
        Value = x.Description,
        Text = x.Description
    });
    var model = new MyViewModel
    {
        Descriptions = new SelectList(descriptions, "Value", "Text")
    };
    return View(model);
}

and finally in your strongly typed view:

<%= Html.DropDownListFor(x => x.SelectedDescription, Model.Decriptions) %>

UPDATE:

After posting your updated models (which by the way are still incomplete and impossible to compile as you haven't provided any property names), here's an example:

public class ViewModel
{
    public int SelectedId { get; set; }
    public Detail Detail { get; set; }
}

public class Detail
{
   public CustomClassCollection MyCollection { get; set; }
}


public class CustomClassCollection
{
   public List<CustomClass> CustomClass { get; set; }
}

public class CustomClass 
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var vm = new ViewModel
        {
            Detail = new Detail
            {
                MyCollection = new CustomClassCollection
                {
                    CustomClass = new List<CustomClass>
                    {
                        new CustomClass
                        {
                            Id = 1,
                            Description = "description 1",
                        },
                        new CustomClass
                        {
                            Id = 2,
                            Description = "description 2",
                        },
                        new CustomClass
                        {
                            Id = 3,
                            Description = "description 3",
                        },
                    }
                }
            }
        };
        return View(vm);
    }
}

and in the view:

<%= Html.DropDownListFor(
    x => x.SelectedId, 
    new SelectList(Model.Detail.MyCollection.CustomClass, "Id", "Description")
) %>

What you have to understand in order to define a dropdown list in ASP.NET MVC ius that you need 2 things:

  1. A scalar property to bind the selected value to (SelectedId in my example)
  2. A collection to bind the list to (Model.Detail.MyCollection.CustomClass in the example)
Sign up to request clarification or add additional context in comments.

10 Comments

That doesnt work I can not select past Custom collection. It then begins to give me options such as select,where...
@Antarr Byrd, how does this custom collection look like? The code you have shown won't even compile. Please provide a complete description of the objects involved in your objects hierarchy.
I will post modified code above, the data is being accessed via a webservice not LINQ
@Antarr Byrd, I have updated my answer to reflect on your modifications. All that is left is now to adapt this code to match your repository structure.
Lets say I have a repo function that calls GetCustomClassCollection from the webservice. How would I format that inside the controller
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.