使用存储库类时的 Html.Dropdownlist 值
到目前为止,我已经使用 Model 类嵌入相关值列表以在视图中呈现为 DropDownList。例如,假设我有一个 Contact 模型类,其中包含 ContactType 属性
public class Contact {
...
public int ContactTypeID { get; set; }
...
}
ContactTypeID 引用数据库表中的一行,该表包含所有联系人类型(如客户、供应商等)。到目前为止,我已经成功使用了 Contact 类的属性例如,如以下示例所示,称为 ContactTypeList 这样
public IEnumerable<SelectListItem> ContactTypeList {
get {
return GetContactTypeList().Select(
t => new SelectListItem { Text = t.Description,
Value = t.ContactTypeID.ToString() });
}
}
,当使用强类型视图时,我可以执行以下操作。
<%: Html.DropDownListFor( model => model.ContactTypeID, Model.ContactTypeList, new {} )%>
此方法可以正常工作,没有任何问题,但从 SoC 的角度来看,它并不完全干净。
在一个新项目中,我开始使用存储库模式和 StructureMap 作为 DI/IoC 工具,因此模型类不再引用从底层数据存储获取数据的存储库类。
我知道我总是可以使用控制器和 ViewData/ViewBag 来让这些列表传递到视图中,但我想知道这是否是实现范围的好方法。
您的项目进展如何?实现结果并保持代码干净的最佳方法是什么?
感谢您的帮助
Until now I have used the Model class to embed list of related values to render as DropDownList in the view. For example, suppose I have a Contact model class that has a ContactType property in it
public class Contact {
...
public int ContactTypeID { get; set; }
...
}
ContactTypeID reference a row in a database table that contains all the contact types like Customer, Supplier, etc. I have used successfully until now a property of the Contact class called, for example, ContactTypeList as in the following sample
public IEnumerable<SelectListItem> ContactTypeList {
get {
return GetContactTypeList().Select(
t => new SelectListItem { Text = t.Description,
Value = t.ContactTypeID.ToString() });
}
}
This way when using strong typed views I can do the following way
<%: Html.DropDownListFor( model => model.ContactTypeID, Model.ContactTypeList, new {} )%>
This method works wothout any problem but is not completely clean from the SoC point of view.
In a new project I am starting I am using the repository pattern and StructureMap as the DI/IoC tool and so the model class, does not have anymore a reference to the repository class that is getting the data from the underlying data store.
I know that I can always use the controller and the ViewData/ViewBag to get these lists to pass in the views but I was wondering if this is a good way to achieve the scope.
How are you doing in your projects? What is the best way to achieve the result and having the code remain clean?
Thanks for helping
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我们的项目中我们也使用ViewData来存储列表并在视图中使用它们,这没有什么问题。
In our project we also use ViewData to store the list and use them in the view, there's nothing wrong in it.
在我的项目中,我使用视图模型和控制器来操作模型:
模型:
控制器:
使用的 AutoMap 属性有一个操作过滤器,它将在控制器操作之后执行,并且它将使用 AutoMapper 来替换模型通过相应的视图模型 (
ContactViewModel
) 传递给视图 (Contact)。您可能会在我编写的示例项目结构中看到它的实际应用。现在显然,如果您要在整个项目中显示此联系人下拉列表,则它需要一个单独的控制器和一个子操作,该操作将通过 Html.Action 助手。
In my projects I use view models and the controller to manipulate the model:
Model:
Controller:
The
AutoMap
attribute used there is an action filter which will execute after the controller action and it will use AutoMapper to replace the model passed to the view (Contact) with the corresponding view model (ContactViewModel
). You may see it in action in a sample project structure I wrote.Now obviously if you are going to show this contacts drop down list all over your project it deserves a separate controller and a child action which will be reused across your view with the Html.Action helper.