Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

c# - MVC Model with a list of objects as property

I am working on an MVC application where the Model class Item has a List<Colour> named AvailableColours as a property.

AvailableColours is a user defined subset of Colour classes. I would like to display all Colour instances in a check box list, and when submitted, AvailableColours is a List<Colour> containing the checked Colour classes.

What is the best way to do this in MVC?

Edit: My code so far, although I feel this is not the most MVC-ish way to do it!

Model

public class Item
{
    public int ID { get; set; }
    public List<Colour> AvailableColours { get; set; }
}

View

@model MyNamespace.Models.Item
@using MyNamespace.Models;
@{
    ViewBag.Title = "Create";

    var allColours = new List<Colour>(); //retrieved from database, but omitted for simplicity
}

<h2>Create New Item</h2>

@using (Html.BeginForm("Create", "Item", FormMethod.Post)) 
{
    <div>
        @Html.LabelFor(model => model.AvailableColours)

        @foreach (var colour in allColours)
        {

           <input type="checkbox" name="colours" value="@colour.Description" />
        }
    </div>

    <input type="submit" value="Submit" />
}

Controller

[HttpPost]
public ActionResult Create(Item item, string[] colours)
{
    try
    {
        foreach (var colour in colours)
        {
            item.AvailableColours.Add(GetColour(colour));//retrieves from database

            return RedirectToAction("Index");
        }
    }
    catch
    {
       return View();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Models

public class Item
{
   public List<Colour> AvailableColours { get;set; }
}

public class Colour
{
    public int ID { get; set; }
    public string Description { get; set; }
    public bool Checked { get; set; }

}

Note the Checked property

View for loop

@using (Html.BeginForm("Create", "Item", FormMethod.Post)) 
{
   <div>
    @Html.LabelFor(model => model.AvailableColours)
    @for(var i = 0; i < Model.AvailableColours.Count; i++)
    {    

        @Html.HiddenFor(m => Model.AvailableColours[i].ID)
        @Html.HiddenFor(m => Model.AvailableColours[i].Description)
        @Html.CheckBoxFor(m => Model.AvailableColours[i].Checked)
        @Model.AvailableColours[i].Description<br/>
     }
    </div>
<input type="submit" value="Submit" />
}

Note the for loop insted of foreach to enable model binding and the hidden fields to allow the values to be posted back to the controller

Model Binding To A List

Controller post

[HttpPost]
public ActionResult Create(Item model)
{
    //All the selected are available in AvailableColours

    return View(model);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...