Onilol
Onilol

Reputation: 1339

How can I set multiple options on a model attribute

I'm trying to elaborate a model ( User for instance ) which can select a color from multiple options ( list )

How could I write it in the model and expose it in the view ? I'd need to write a separate model only for the colors or is this approach going the right way :

//user.cs
public class User
{
    public string Name { get; set; }

    public Colors Colors { get; set; }
}

//colors.cs
public class Colors
{
    public User User { get; set; }
    public enum Colors ???
}

Upvotes: 0

Views: 464

Answers (2)

Brendan Vogt
Brendan Vogt

Reputation: 26018

Your basic user class will look like this. I broke down your name property to first name and last name.

public class User
{
     public string FirstName { get; set; }

     public string LastName { get; set; }
}

I changed your colour class a bit in case (one day) you want to be able to go the database route instead of having a fixed set of colours. Name your class in singular form, not plural, for example Colour instead of Colours.

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

     public string Name { get; set; }
}

A user view model will be passed to the view. This view model will contain user information and a list of the colours that will be used in the dropdown.

public class UserViewModel
{
     public string Name { get; set; }

     public int ColourId { get; set; }

     public List<Colour> Colours { get; set; }
}

In your controller's action method you create an instance of the view model above and populate it with user data and a list of all the colours.

public ActionResult Edit(int userId)
{
     // Get your user's information
     User user = userRepository.GetById(userId);

     UserViewModel model = new UserViewModel();
     model.Name = user.FirstName + " " + user.LastName;
     // This can be separated into a repository class of its own if you need to use colours in another action method
     model.Colours = new List<Colour>
     {
         new Colour { Id = 1, Name = "Blue" },
         new Colour { Id = 2, Name = "Green" },
         new Colour { Id = 3, Name = "Red" },
         new Colour { Id = 4, Name = "Yellow" }
     };

     return View(model);
}

And then in your view you will have something like:

<h1>@Model.Name</h1>
<p>Please select a colour:</p>
@Html.DropDownListFor(
     x => x.ColourId,
     new SelectList(Model.Colours, "Id", "Name", Model.ColourId),
     "-- Select --"
)

Play around with the code to see how it works out for you.

Upvotes: 1

Rhys
Rhys

Reputation: 2173

You're nearly there. The Color class isn't necessary if you're only wanting to provide a Color enum. Taking that into account, try this:

public enum Color
{
    Red,
    Green,
    Blue
}

public class User
{
    public String Name { get; set; }

    public Color Color { get; set; }
}

public class UserColorAddEditViewModel
{
    public String Name { get; set; }

    public Color Color { get; set; }

    public IList<SelectListItem> ColorSelectList { get; set; }
}

Then in the View, you can use the UserColorAddEditViewModel model and use the following HtmlHelper:

@Html.DropDownListFor(model => model.Color, Model.ColorSelectList)

or as Icemanind mentioned, you can simply use:

@Html.EnumDropDownListFor(model => model.Color) and you scrap the ColorSelectList variable (that means you can ignore the part below too).

If you decide to use the DropDownListFor helper, you'll need to initiate this variable within the defined Controller class, as shown below.

public class MyController : Controller
{
     public ActionResult Index()
     {
          UserColorAddEditViewModel Model = new UserColorAddEditViewModel();

          Model.ColourSelectList = Enum.
                GetValues(typeof(Color)).
                Cast<Color>().
                Select(c => new SelectListItem { Text = c.ToString(), Value = ((int)c).ToString()})
                .ToList();

        return View(Model);
    }
}

Upvotes: 2

Related Questions