I'm working with a Razor MVC3 project
I have a model
public class ProductFormatModel
{
public int ID {get; set;}
public string SerialID { get; set; }
public ProductTypes ProductType { get; set; }
public List<ProductComponent> Components { get; set; }
}
The ProductComponent class has two properties: int ID and string Description.
I would like the controller to work with a strongly typed view of ProductFormatModel type to create a new ProductFormatModel. The Create view should look like th开发者_开发知识库is:
- A textfield to insert SerialID (done)
- A dropdownlist to select the value of the enumerator ProductType (done)
- A listbox with different rows showing the "Description" property of ProductComponent. Every row should present a record in the database. The user must be able to select one or more rows and in this way the property "Components" should have those objects in the list.
The database is a simple DbContext. MyDB.Components gives me the recordset of table "Components"
How do I pass the values of the recordset from the controller to the view? Should I call a listboxfor with multiselectlist? Should I add another property to the model like List ComponentsToBeSelected in which I can pass the list?
One possible design:
Use two listboxes in your view. Load the first with all possible ProductComponent
s that are not in model.Components
, and use ListBoxFor
to bind the second to the model.Components
list.
Place two buttons, Add and Remove, between the lists. Wire these up with jQuery to move items between the two listboxes.
精彩评论