如何填充数据列表框,并保存到数据库中的ASP.NET mvc5 [英] how to populate data to listbox and save to database in asp .net mvc5
问题描述
我有2个模型类。一个是Book.cs,另一个是Cateogry.cs。图书和类别有许多一对多的关系。所以,我创建了另一个视图模型BookCategory.cs。我想创建内部BookController.cs的类别列表框和创建书/ Edit.cshtml页面应该有列表框为那里的一本书可以与类别的多项选择创建/保存/编辑数据库一起类别。这里是我的模型codeS。默认BookController的给我只为类别的DropDownList。怎样才可以有列表框呢?请给我任何suggesion。我刚进入MVC仅为2周。谢谢了很多。
Book.cs
公共类图书
{
公众诠释BOOKID {搞定;组; } 公共字符串BOOKNAME {搞定;组; } 公共虚拟的ICollection<类别及GT;分类{搞定;组; }
Category.cs
公众诠释的CategoryId {搞定;组; } 公共字符串类别名称{搞定;组; } 公共虚拟的ICollection<图书>图书{搞定;组; }
我的视图模型为BookCategoryVM.cs
公共类BookCategoryVM
{
公众诠释的CategoryId {搞定;组; }
公共字符串类别名称{搞定;组; }
公共BOOL IsSelected {搞定;组; } 公共IEnumerable的<图书>图书{搞定;组; }
公共IEnumerable的<类别及GT;分类{搞定;组; }
}
您创建或编辑的书需要包括对所选类别的属性视图模型
公共类BookVM
{
公众诠释ID {搞定;组; } //只需要编辑现有图书
[需要]
公共字符串名称{;组; }
[显示(名称=分类)]
[必需(的ErrorMessage =请至少选择一个类别)]
公众诠释[] {SelectedCategories获得;组; }
公众的SelectList所属分类{搞定;组; }
}
查看
@ Html.LabelFor(M = GT; m.SelectedCategories)
@ Html.ListBoxFor(M = GT; m.SelectedCategories,Model.CategoryList)
@ Html.ValidationMessageFor(M = GT; m.SelectedCategories)
控制器
公众的ActionResult的Create()
{
BookVM模式=新BookVM();
IEnumerable的<类别及GT;类别= db.Categories; //修改以满足您的要求
model.CategoryList =新的SelectList(类别的CategoryId,类别名称)
返回查看(模型);
}公众的ActionResult创建(BookVM模型)
{
// model.SelectedCategories将包含在列表框中选择的每个类别的ID
}
I have 2 model class. One is Book.cs and the other is Cateogry.cs. Book and Category has many-to-many relationship. So, I created another viewmodel BookCategory.cs. I want to create a listbox for Category inside BookController.cs and Create/Edit.cshtml pages of Book should have listbox for Category where a book can be created/saved/edit to database together with multiselection of category. Here is my model codes. Default Bookcontroller gives me only dropdownlist for category. How can I have listbox instead? Please give me any suggesion. I just get into MVC for 2 weeks only. Thank a lot.
Book.cs
public class Book
{
public int BookId{ get; set; }
public string BookName { get; set; }
public virtual ICollection<Category> Categories { get; set; }
Category.cs
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Book> Books { get; set; }
My ViewModel for BookCategoryVM.cs
public class BookCategoryVM
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public bool IsSelected { get; set; }
public IEnumerable<Book> Books { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
Your view model for creating or editing a book need to include a property for the selected categories
public class BookVM
{
public int ID { get; set; } // only required for editing existing book
[Required]
public string Name { get; set; }
[Display(Name = "Categories")]
[Required(ErrorMessage = "Please select at least one category")]
public int[] SelectedCategories { get; set; }
public SelectList CategoryList { get; set; }
}
View
@Html.LabelFor(m => m.SelectedCategories)
@Html.ListBoxFor(m => m.SelectedCategories, Model.CategoryList)
@Html.ValidationMessageFor(m => m.SelectedCategories)
Controller
public ActionResult Create()
{
BookVM model = new BookVM();
IEnumerable<Category> categories = db.Categories; // modify to suit your requirements
model.CategoryList = new SelectList(categories, "CategoryId", "CategoryName")
return View(model);
}
public ActionResult Create(BookVM model)
{
// model.SelectedCategories will contain the ID of each category selected in the listbox
}
这篇关于如何填充数据列表框,并保存到数据库中的ASP.NET mvc5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!