哪里是格式化ASP.NET MVC(3)型号性能最好的地方? [英] Where is the best place to format Model properties in ASP.NET MVC(3)?

查看:103
本文介绍了哪里是格式化ASP.NET MVC(3)型号性能最好的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在寻找了很多的ASP.NET MVC框架内的最佳实践,特别是围绕关注sparation。我在这里的问题是,如果我有一个模型的属性,在哪里做对即物业某些格式的最佳地点?

I've been looking a lot recently as best practices within the ASP.NET MVC framework, specifically around sparation of concern. My question here is, if I have a property on a model, where is the best place to do some formatting on that property?

我已经想过了几个选择,但我不知道这是最好的。该示例使用日期时间。很抱歉,这有一点长。

I've thought about a few options but am not sure which is best. The example uses a DateTime. Sorry this got a bit long.

选项1:
在视图: @ Model.TheDate.String({0:'blah'dd / MM / YYYY})
我知道这是不对的,因为格式字符串不应该在这里(如果我想改变在整个应用程序的格式)

Option 1: In the view: @Model.TheDate.String("{0:'blah'dd/MM/yyyy}") I know this isn't right because the format string shouldn't be here (what if I want to change the format throughout the whole app)

选项2:
在视图中使用的扩展方法: @ Model.TheDate.DisplayItNicePlease()
这种品牌的意义,因为该视图艇员选拔使用格式化的扩展方法和视图模型只是公开日期属性的看法。不知道,如果视图或视图模型应该负责,虽然格式。

Option 2: Extension method used within the view: @Model.TheDate.DisplayItNicePlease() This kind of makes sense because the view is chosing the extension method to use for formatting and the viewmodel just exposes the date property to the view. Not sure if the view or view model should be responsible for formatting though.

选项3:
在HTML辅助方法,即像<一个href=\"http://stackoverflow.com/questions/3981175/where-should-i-place-declarative-html-helpers-in-asp-net-mvc-3\">Where我应该把声明HTML佣工在ASP.NET MVC 3 ,所以你可以使用:

@Html.DisplayDateNice(Model.TheDate)

选项4:
在视图模型额外的属性进行格式化,并返回一个字符串,例如:

Option 4: Extra property on the view model to format it and return a string e.g.:

public string TheDateStr
{
    get
    {
        return TheDate.DisplayItNicePlease();
    }
}

然后在视图中,您只需

And then in the view you just have

@Model.TheDateStr

非常相似的选项只有3保持它的观点非常非常简单 - 视图真的只是输出是什么模型,并且不关心数据的格式为:

Fairly similar to option 3 only it keeps the view really, really simple - the view literally just outputs what is in the model and doesn't care about the format of the data.

方法5:
DisplayFormat属性,例如:
在视图模型:

Option 5: DisplayFormat attributes, e.g.: On the view model:

[DisplayFormat(DataFormatString = "{0:'blah'dd/MM/yyyy}")]
public DateTime TheDate { get; set; }

,然后在视图:

@Html.DisplayFor(m => m.TheDate)

我喜欢这除了再次格式字符串不重用,因此带来的选项5 ...

I like this apart from again the format string is not reused, so bring on option 5...

选项6:
某种自定义的 DisplayFormatAttribute 例如 NiceDateDisplayFormatAttribute
这似乎对我来说,这可能是最好的解决方案,工作得很好,如果你只是想要一个简单的字符串格式化。我认为,如果你想更复杂的东西,比如显示相对时间这个问题变得更为复杂(http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time),在这种情况下,也许这是最好的在扩展方法/ HTML辅助之中。

Option 6: Some kind of custom DisplayFormatAttribute e.g. NiceDateDisplayFormatAttribute. This seems to me like it might be the nicest solution and works well if you just want a simple string formatting. I think this gets more complicated if you want something more complicated, e.g showing relative time (http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time), in which case maybe this is best being in an extension method/html helper.

有可能是其他的方式,我甚至不知道...

There are probably other ways I'm not even aware of...

推荐答案

要么使用一个扩展帮手格式化类型,如果这种类型的格式将被用于大量或模型添加额外的属性返回的格式化版本原来的属性。

Either use an extension helper to format the type if this type formatting will be used a lot or add an extra property in the Model to return a formatted version of the original property.

这篇关于哪里是格式化ASP.NET MVC(3)型号性能最好的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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