Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将视图特定信息放在何处?_C#_Asp.net_Presenter - Fatal编程技术网

C#将视图特定信息放在何处?

C#将视图特定信息放在何处?,c#,asp.net,presenter,C#,Asp.net,Presenter,我试图减少asp.net web表单中存在的代码重复。这就是从数据库加载的示例对象的外观 Apartment int id decimal rent bool callForPricing int squareFeet int beds int baths 现在,我在几个不同的asp.net页面(即包含多个单元的列表、详细视图等)中从该对象创建视图。在过去,我所做的是创建另一个封装公寓类的类。像这样的 ApartmentView Apartment apt

我试图减少asp.net web表单中存在的代码重复。这就是从数据库加载的示例对象的外观

Apartment
  int id
  decimal rent
  bool callForPricing
  int squareFeet
  int beds
  int baths
现在,我在几个不同的asp.net页面(即包含多个单元的列表、详细视图等)中从该对象创建视图。在过去,我所做的是创建另一个封装公寓类的类。像这样的

ApartmentView

  Apartment apt

  public virtual String Rent
  {
     get 
     { 
        if (apt.CallForPricing)
        {
           return "Call For Pricing"; 
        }else{
           return apt.Rent.ToString("C") + "/Month";
        }
     }
  }

  public virtual String BedsBathsSqFt
  {
     get 
     { 
        if (apt.squareFeet > 0)
        {
           return apt.beds + " Beds|" + apt.beds + " Beds|" + apt.SquareFeet + " sqft"; 
        }else{
           return apt.beds + " Beds|" + apt.beds + " Beds";
        }
     }
  }

正如您所看到的,我通常只是创建数据的字符串表示。我曾考虑过让ApartmentView类扩展公寓类,但由于“租金”等重叠属性,我没有这样做。我只是想知道人们通常如何处理这种情况。这是正确的命名约定吗

在任何webforms项目中,这都是一个很难解决的难题。对于条件格式逻辑,您有两种选择——在页面本身(现在您必须在整个站点上复制它),或者像您正在做的那样在一些代码中。在关注点分离方面,两者都不是很好的选择,这是ASP.NETWebForms模型的一个众所周知的缺点

就我个人而言,我会将您的视图转换为包含FormView控件的.ascx web用户控件,并将其数据绑定到公寓包装器对象。比如:

<asp:FormView ID="FormView1"
  DataSourceID="ObjectDataSource1"
  RunAt="server">

  <ItemTemplate>
    <table>
      <tr>
        <td align="right"><b>Rent:</b></td>       
        <td><%# Eval("Rent") %></td>
      </tr>
      <tr>
        <td align="right"><b>BedsBathsSqFt:</b></td>     
        <td><%# Eval("BedsBathsSqFt") %></td>
      </tr>
    </table>                 
  </ItemTemplate>                 
</asp:FormView>
,人们试图绕过这些限制