开发者

Listboxfor MVC3 Razor. User selection from database fields

开发者 https://www.devze.com 2023-03-10 22:18 出处:网络
I\'m working with a Razor MVC3 project I have a model public class ProductFormatModel { public int ID {get; set;}

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 ProductComponents 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消