ASP.net MVC:在视图或操作中创建 SelectList?

发布于 2024-08-20 18:41:59 字数 275 浏览 5 评论 0原文

我只是想知道人们在哪里创建他们的 SelectList - 在操作或视图中。

我见过这两种情况的示例,对我来说最有意义的一个是在操作中执行此操作,并使视图模型具有 SelectList 类型的属性。

另一方面,我见过一些示例,其中人们让视图模型具有 SelectList 属性,并且 SelectList 填充在视图模型中(在构造函数中或通过延迟加载)。我喜欢这个想法,因为这意味着我的操作中的代码更少......

简而言之,我只是想知道人们在做什么 atm。

干杯安东尼

I'm just wondering where people are creating their SelectList - in the action or the view.

I have seen examples of both and the one that makes the most sense to me is doing it in the action and have the view model have a property of type SelectList.

On the other hand, I have seen examples where people have the view model have a property of SelectList and the SelectList is populated within the view model (either in the constructor or via lazy loading). I like this idea as it means there is less code in my actions...

In short I was just wondering what people are doing atm.

Cheers Anthony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回梦 2024-08-27 18:41:59

在控制器中创建 SelectList(通过从模型存储库查找项目列表),并将其作为 ViewData 对象或强类型 ViewModel 的一部分传递到视图。

Create your SelectList in the controller (by looking up your list of items from your model repository), and pass it to the view as either a ViewData object, or as part of your strongly-typed ViewModel.

橘香 2024-08-27 18:41:59

这是一个特定于演示的方面,因此我更喜欢使用 Html 帮助程序在视图中执行此操作。因此,我将一个集合传递给 View,并使用 html 帮助器方法将项目映射到 SelectListItems。该方法可能看起来非常像这样:

public static IList<SelectListItem> MapToSelectItems<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty, Func<T, string> valueProperty, Predicate<T> isSelected)
{
    var result = new List<SelectListItem>();

    foreach (var item in itemsToMap)
    {
        result.Add(new SelectListItem
        {
            Value = valueProperty(item),
            Text = textProperty(item),
            Selected = isSelected(item)
        });
    }
    return result;
}

问候。

It´s a presentation-specific aspect, so I prefer to do it in the View, using an Html helper. So I pass a collection to the View and use a html helper method to map the items to SelectListItems. The method could look very much like this:

public static IList<SelectListItem> MapToSelectItems<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty, Func<T, string> valueProperty, Predicate<T> isSelected)
{
    var result = new List<SelectListItem>();

    foreach (var item in itemsToMap)
    {
        result.Add(new SelectListItem
        {
            Value = valueProperty(item),
            Text = textProperty(item),
            Selected = isSelected(item)
        });
    }
    return result;
}

Regards.

極樂鬼 2024-08-27 18:41:59

我通常在操作或服务层中创建 SelectList,并通过 ViewData 将其传递到我的视图。我还将它作为视图模型和强类型视图的一部分。这两种方法都在操作或服务层中创建它。

I typically create my SelectList in the action or service layer and pass it to my view via ViewData. I've also made it a part of a view model and a strongly typed view. Both ways create it in the action or service layer though.

我一直都在从未离去 2024-08-27 18:41:59

我将 SelectList 作为属性公开在视图模型中,并使用必要的存储库将其填充到操作中。我认为直接与存储库交互的代码应该也负责填充,无论是控制器操作还是服务层还是其他任何层。

我认为直接从视图模型填充列表不是一个好主意,因为它需要视图模型具有存储库依赖性并进行数据库交互,而视图模型不应该负责此类事情。

如果您有多个 SelectList 字段并希望保持操作代码简洁,您还可以创建一个单独的特殊对象,称为 Initializer 或类似的东西,它可以完成所有填充和初始化。

I have the SelectList exposed as a property in the view model and populate it in the action using the necessary repository. I think that the code that interacts directly with the repositories should be the one that is also responsible with populating, be it the controller actions or service layer or whatever else.

I don't think that populating the list directly from the view model is a good idea, because it would require the view model to have a repository dependency and do database interactions and the view model should not be responsible for this kind of things.

You could also create a separate special object, called Initializer or something like that, that does all the populating and initializations, if you have multiple SelectList fields and want to keep your actions code cleaner.

〃安静 2024-08-27 18:41:59

两者都不是,在单独的类中创建它,请查看此处 如何在 POST 操作中将视图模型映射回域模型?

我在控制器中使用 IBuilder 接口,并在此 Builder 的实现中完成所有实体/视图模型的构建

Neither, create it in a separate class, look here How to map View Model back to Domain Model in a POST action?

I use an IBuilder interface in the controller and do all the building of entities/viewmodels in the implementation of this Builder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文