带有下拉列表的 ASP.Net MVC 3 ViewModel
我正在开发 ASP.Net MVC 3 Web 应用程序。该应用程序当前连接到一个具有多个表的数据库,其中两个表是 Category(catId, Name) 和 Site(siteID, Name)。
我希望创建一个具有两个下拉列表的视图,一个下拉列表对应所提到的每个表,以便用户可以从中进行选择,然后运行报告。为此,我创建了一个 viewModel 来表示两个下拉列表
public class ReportSiteCategorySearchViewModel
{
public SelectList categoryList { get; set; }
public SelectList siteList { get; set; }
}
然后在返回 viewModel 的控制器中我有以下内容
public ActionResult getEquipmentByCategoryAndSite()
{
ReportSiteCategorySearchViewModel viewModel = new ReportSiteCategorySearchViewModel
{
categoryList = new SelectList(categoryService.GetAllCategories().ToList(), "categoryID", "categoryTitle"),
siteList = new SelectList(siteService.GetAllSites().ToList(), "siteID", "title")
};
return View(viewModel);
}
,然后传递给一个视图,该视图采用此 viewModel 并将值写出到下拉列表中
<div>
<label for="ddlSite">Sites</label>
@Html.DropDownList("ddlSite", Model.siteList, "All Sites")
<label for="ddlCatgeory">Categories</label>
@Html.DropDownList("ddlCatgeory", Model.categoryList, "All Categories")
</div>
这可以工作,但是,我不确定这是最好的方法。我只是想知道我的方法是否正确,有更好的方法吗?即,如果我需要其他表中的 5/6 更多下拉列表,我应该添加到当前的 viewModel 等吗?
任何反馈将不胜感激。
谢谢。
I am developing an ASP.Net MVC 3 web application. The app currently is connected to a database that has several tables, two of which are Category(catId, Name) and Site(siteID, Name).
I wish to create a view that has two drop down lists, one for each of the tables mentioned, so that the user can select from and then run a report. To do this I have created a viewModel to represent the two drop down lists
public class ReportSiteCategorySearchViewModel
{
public SelectList categoryList { get; set; }
public SelectList siteList { get; set; }
}
Then in my controller that returns the viewModel I have the following
public ActionResult getEquipmentByCategoryAndSite()
{
ReportSiteCategorySearchViewModel viewModel = new ReportSiteCategorySearchViewModel
{
categoryList = new SelectList(categoryService.GetAllCategories().ToList(), "categoryID", "categoryTitle"),
siteList = new SelectList(siteService.GetAllSites().ToList(), "siteID", "title")
};
return View(viewModel);
}
I then pass to a view which takes this viewModel and writes out the values to the drop downs
<div>
<label for="ddlSite">Sites</label>
@Html.DropDownList("ddlSite", Model.siteList, "All Sites")
<label for="ddlCatgeory">Categories</label>
@Html.DropDownList("ddlCatgeory", Model.categoryList, "All Categories")
</div>
This works, however, I am not sure this is the best way to do it. I am just wondering is my method correct, is there a better way to do this? Ie, what if I needed 5/6 more drop down lists from other tables, should I just add to the current viewModel etc?
Any feedback would be much appreciated.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个
List
类型的 viewModel 在控制器中,将每个表(如您所做的那样作为 SelectList)添加到该模型中。然后将视图传递给模型,它是 SelectList 的列表。然后您可以迭代视图中的每个值:
您可能需要修改 SelectList 列表以包含“标题”或“项目”字段。通过这种方式,您可以不断向列表添加元素,并且不需要更新视图。
You can create a viewModel of type
List<SelectList>
In your controller, add each table (as a SelectList as you're doing) to this model. Then pass the view the model, which is a list of SelectLists.Then you can iterate through each value in your view:
You may need to modify your list of SelectList to include the 'Title' or 'items' field. By doing it this way you can keep adding elements to the List, and you won't need to update the view.