Chr*_*xon 2 .net c# asp.net-mvc
我期望在.NET MVC中实现它,但试图弄清楚如何实际执行它.目前在我的ViewModel上,我有(例如):
public class GroupPolicyViewModel
{
public int PolicyId { get; set; }
public int HistoryId{ get; set; }
public SelectList ProductList { get; set; } // tried this
public List<Product> ProductList1 { get; set; } // tried this
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试从此ViewModel自动生成View时,ProductList都会被忽略.有没有办法从ViewModel自动生成DropDownList?
随着模型
public class GroupPolicyViewModel
{
public int PolicyId { get; set; }
public int HistoryId{ get; set; }
public int SelectedProductId{ get; set; }
public List<Product> ProductList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您可以创建DropDownList
@Html.DropDownListFor(m => m.SelectedProductId,
new SelectList(Model.ProductList, "ProductId", "ProductName"))
Run Code Online (Sandbox Code Playgroud)
或者,如果您的模型中有SelectList产品
@Html.DropDownListFor(m => m.SelectedProductId, Model.ProductSelectList)
Run Code Online (Sandbox Code Playgroud)
如果您想要一些生成的代码,则需要使用scaffolding选项来提供数据上下文类.这是一个很好的教程MVC音乐商店