如何使用select方法绑定razor视图中viewmodel的下拉列表 [英] How to bind dropdown list from viewmodel in razor view with select method
问题描述
我想创建一个剃须刀视图,其中包含一个下拉列表,根据类别中选择的类别名称显示类别名称和产品列表。
模型类: -
公共类InventoryContext:DbContext
{
public InventoryContext()
:base(DefaultConnection)
{} DbSet< categorymodel>类别{get;组; }
public DbSet< productmodel>产品{get;组; }
}
[表(类别)]
公共类CategoryModel
{
[Key]
public int CategoryId
{
get;
set;
}
[StringLength(50)]
[必需(AllowEmptyStrings = false)]
公共字符串CategoryName
{
get;
set;
}
public string CategoryDesc
{
get;
set;
}
public DateTime AddedOn
{
get;
set;
}
[必需(AllowEmptyStrings = false,ErrorMessage = 选择状态)]
public int状态
{
get;
s et;
}
}
[表(产品)]
公共类ProductModel
{
// int categoryId;
[Key]
public int ProductId
{
get;
set;
}
[StringLength(50)]
[必需(AllowEmptyStrings = false)]
公共字符串ProductName
{
get;
set; < br $>
}
公共字符串ProductDesc
{
get;
set;
}
public DateTime AddedOn
{
get;
设置;
}
[必需(AllowEmptyStrings = false,ErrorMessage =选择状态)]
public int Status
{
get;
set;
}
// [ForeignKey(Product_CategoryId) )]
public int CategoryId
{
get;
set;
}
}
公共类InventoryModels
{
public List< categorymodel> IM_CategoryModel {get;组; }
public List< productmodel> IM_ProductModel {get;组; }
公共InventoryModels(List< categorymodel> _categoryModel,List< productmodel> _productModel)
{
IM_CategoryModel = _categoryModel;
IM_ProductModel = _productModel;
}
}
ProductController.cs的控制器动作: - < br $>
公共ActionResult索引(int id = 0)
{
var productList = db.Product.Where( q => q.CategoryId == id || id == 0)。ToList();
List< categorymodel> categoryList = db.Category.ToList(); // new SelectList(db.Category,CategoryId,CategoryName,db.Category);
InventoryModels objInventoryModels = new InventoryModels(categoryList,productList);
返回查看(objInventoryModels);
}
Razor查看: -
@model MvcAppTest.Models.InventoryModels
请帮我填写下面的类别下拉列表。
注意:我没有发布视图代码的产品列表因为它简单且工作正常。
只帮我从我的viewmodel创建下拉列表
@ * @ {
@ Html.DropDownList(IM_CategoryModel [0] .CategoryId,Model.IM_CategoryModel);
} * @
I want to create a razor view with one dropdown list which shows category names and list of products based on the category name selected in category drop.
Model class: -
public class InventoryContext : DbContext
{
public InventoryContext()
: base("DefaultConnection")
{} DbSet<categorymodel> Category { get; set; }
public DbSet<productmodel> Product { get; set; }
}
[Table("Category")]
public class CategoryModel
{
[Key]
public int CategoryId
{
get;
set;
}
[StringLength(50)]
[Required(AllowEmptyStrings = false)]
public string CategoryName
{
get;
set;
}
public string CategoryDesc
{
get;
set;
}
public DateTime AddedOn
{
get;
set;
}
[Required(AllowEmptyStrings = false, ErrorMessage = "Select status")]
public int Status
{
get;
set;
}
}
[Table("Product")]
public class ProductModel
{
//int categoryId;
[Key]
public int ProductId
{
get;
set;
}
[StringLength(50)]
[Required(AllowEmptyStrings = false)]
public string ProductName
{
get;
set;
}
public string ProductDesc
{
get;
set;
}
public DateTime AddedOn
{
get;
set;
}
[Required(AllowEmptyStrings = false, ErrorMessage = "Select status")]
public int Status
{
get;
set;
}
//[ForeignKey("Product_CategoryId")]
public int CategoryId
{
get;
set;
}
}
public class InventoryModels
{
public List<categorymodel> IM_CategoryModel { get; set; }
public List<productmodel> IM_ProductModel { get; set; }
public InventoryModels(List<categorymodel> _categoryModel, List<productmodel> _productModel)
{
IM_CategoryModel = _categoryModel;
IM_ProductModel = _productModel;
}
}
Controller Action of ProductController.cs: -
public ActionResult Index(int id = 0)
{
var productList = db.Product.Where(q => q.CategoryId == id || id == 0).ToList();
List<categorymodel> categoryList = db.Category.ToList(); // new SelectList(db.Category, "CategoryId", "CategoryName", db.Category);
InventoryModels objInventoryModels = new InventoryModels(categoryList, productList);
return View(objInventoryModels);
}
Razor View:-
@model MvcAppTest.Models.InventoryModels
Please help me to complete below dropdownlist for category.
Note: I am not posting product list of code of view coz it is simple and working fine.
Only help me to create dropdown list from my viewmodel
@*@{
@Html.DropDownList("IM_CategoryModel[0].CategoryId", Model.IM_CategoryModel);
}*@
推荐答案
这篇关于如何使用select方法绑定razor视图中viewmodel的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!