应该在ASP.Net MVC中查看模型是否都是字符串?

6 asp.net-mvc

我有点(无意中)我觉得在某些部分我在视图(.aspx)本身做太多,格式化,连接太多,在一个地方有一些正则表达式替换.

我开始研究一个新的部分,并试图改进我的方法..然后它打我,为什么我不只是制作我的所有视图模型(在/ Models/in .Web项目中)字符串或一个字符串列表在一个推.注意:我不是指我的模型/域,而是指我的ViewModel.

public class FinanceQuoteView
{
    public string Provider; 
    public string Broker; // rather than Broker == null ? "N/A" : Broker.ToUpperCase();
    public string Monthly; // rather than Monthly.ToString("C")
    public string PaymentTerm; // rather than "1+" + PaymentTerm.ToString();
    public string FreeInsurance; // rather than insuranceIncluded ? "Yes" : "No";
    public string[] Restrictions;
}
Run Code Online (Sandbox Code Playgroud)

对于表单提交(添加编辑),我使用单独的视图模型来提供控制器操作(表单模型,如果您将在/ Models/Form中).所以FinanceQuoteForm包含双打等...通过活页夹构建.

大家对这种方法的看法是什么?在从域到视图模型的映射中做.ToString("C")太多了吗?

cjk*_*cjk 2

您的模型应该生成正确的数据,然后由视图以所需的格式生成数据。如果您在模型之上构建了另一个视图,您可能想要对数据进行不同的操作,因此我建议不要将它们作为模型中的字符串返回。