应该在哪里选择列表的逻辑坐在ASP.NET MVC,视图模型或控制器? [英] Where should selectlist logic sit in ASP.NET MVC, view, model or controller?

查看:103
本文介绍了应该在哪里选择列表的逻辑坐在ASP.NET MVC,视图模型或控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我的问题是接近这一个,但我想在哪里code这样应该坐在一个更广泛的讨论。 <一href=\"http://stackoverflow.com/questions/2149855/asp-net-mvc-selectlist-refactoring-question\">http://stackoverflow.com/questions/2149855/asp-net-mvc-selectlist-refactoring-question

I feel my question is close to this one, but I want a more general discussion on where code like this should sit. http://stackoverflow.com/questions/2149855/asp-net-mvc-selectlist-refactoring-question

我目前直接创建我的实体模型我selectlists,像这样。

I currently create my selectlists directly on my entity model, like so.

public SelectList taskDeadlineTime
    {
        get { return new SelectList(TimeDictionary, "Value", "Key", this.getDeadlineTime()); }
    }

这感觉有点不对劲,因为如果我执行视图的工作,我的模型中。

This feels a bit wrong, as if I am performing view work, inside my model.

但它确实意味着我可以刚刚得到的财产,我的选择列表是存在的。

However it does mean that I can just get the property, and my selectlist is there.

现在,我应该把这个逻辑在我的控制器(更多code写的)或视图(感觉不对劲,杂乱),或者只是做它用不同的方式。

Now, should I put this logic in my controller (more code to write) or view (feels wrong, and messy) or just be doing it in a different way.

我现在正在这样做的原因,是因为我比较与同一对象实体的两份工作,并有选择列表作为吸气直接意味着它不能正常工作的一部分。我知道我可以修改此相比,处理这个问题,但它只是觉得不妥做一些视觉模型(除非选择列表中的preparation是在模型中有正确的东西)

The reason I am looking this now, is because I am working with comparing two copies of the same object entity, and having select lists as part of the getter directly means it doesn't work. I know I could modify this comparison to handle this, but it just feels wrong to do something visual in the model (unless the preparation of the select list is the correct thing to have in the model)

推荐答案

我通常把进入查看。

视图模型:

public IEnumerable<Foo> TaskDeadlineTimes { get; set; }

查看:

<%= Html.DropDownListFor(
    x => x.SelectedValue, 
    new SelectList(Model.TaskDeadlineTimes, "Value", "Key")
) %>

和控制器需要使用一个仓库设置该属性值的照顾。

And the controller takes care of setting this property value using a repository.

这篇关于应该在哪里选择列表的逻辑坐在ASP.NET MVC,视图模型或控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆