【发布时间】:2026-01-11 21:30:02
【问题描述】:
我的意思是(无意中)我觉得在某些部分我在视图 (.aspx) 本身内做的太多了,太多的格式,连接,在一个地方有点正则表达式替换。
我开始研究一个新部分并尝试改进我的方法。然后我突然想到为什么我不制作我所有的视图模型(在 /Models/ .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;
}
对于表单提交(添加编辑),我使用单独的视图模型来提供控制器操作(如果您将在 /Models/Form 中使用表单模型)。所以 FinanceQuoteForm 确实包含双打等...通过活页夹构建。
大家对这种方法有什么看法?在从域到视图模型的映射中执行 .ToString("C") 太多了吗?
【问题讨论】:
标签: asp.net-mvc