views:

37

answers:

2

I have the following 2 classes:

public class SomeClass
{
  public int SomeClassID { get; set; }
  ...
}

public class AnotherClass
{
  public int AnotherClassID { get; set; }
  public int AnotherClassText { get; set; }
}

I have a ViewModel that contains the following:

public class MyViewModel
{
  public SomeClass { get; set; }
  public List<AnotherClass> { get; set; }
}

I have a Strongly-Typed View of type MyViewModel. I want to use DropDownListFor() in order to have a list of AnotherClass (value = AnotherClassID, text = AnotherClassText) and have whatever the user selects be assigned to the SomeClass.SomeClassID

How can I accomplish this?

A: 

You can add a property to your ViewModel that gets and sets the SomeClassID, then make the dropdown like this:

Html.DropDownListFor(m => m.SomeClassId, 
    m.OtherClasses.Select(o => new SelectListItem { Text = o.Text, value = o.ID})
);
SLaks
This doesn't seem to work. Issues with casting since SelectListItem.Value wants an int.
Brian David Berman
@Brian: So what's your ID property? Why can't you cast it?
SLaks
m => m.SomeClassId is an int, are you suggesting I ToString() that?
Brian David Berman
@Brian: Yes, I am.
SLaks
+2  A: 

Model:

public class SomeClass
{
    public int SomeClassID { get; set; }
}

public class AnotherClass
{
    public int AnotherClassID { get; set; }
    public int AnotherClassText { get; set; }
}

public class MyViewModel
{
    public SomeClass SomeClass { get; set; }
    public List<AnotherClass> AnotherClasses { get; set; }
}

Controller:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            AnotherClasses = new[] 
            {
                new AnotherClass { AnotherClassID = 1, AnotherClassText = 1 },
                new AnotherClass { AnotherClassID = 2, AnotherClassText = 2 },
                new AnotherClass { AnotherClassID = 3, AnotherClassText = 3 },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // TODO: 
        return View(model);
    }
}

View:

<%= Html.DropDownListFor(x => x.SomeClass.SomeClassID, 
    new SelectList(Model.AnotherClasses, "AnotherClassID", "AnotherClassText"))%>
Darin Dimitrov
@Darin Dimitrov it's correct but do you do it like this in your real projects ? the sending entities to the view, and using the selectlist(,,) inside the view
Omu
Yes, that's how I do it in real projects except that I use a repository to fetch the data instead of hardcoding it but that's obvious. I populate a view model in the controller and pass it to the view. In the view I use SelectList to create the dropdown and choose which properties from the view model to bind to. It is also possible to use directly SelectList as a property of the view model but personally I prefer keeping this logic in the view. It just seems easier when mapping between domain models and view models with AutoMapper to avoid SelectList but it works also like this.
Darin Dimitrov