哪个更适合在 ASP.Net MVC 中显示大量小值 - HTML 帮助器或部分视图

发布于 2024-09-08 08:53:24 字数 1341 浏览 5 评论 0原文

我们有一个小型的 Asp.Net MVC 项目,它显示包含数十或数百行数据的报告。每行都由视图模型上的一个对象表示。

目前我们显示的行如下:

<table>
   <tr style="text-align:center">
        <th>Name</th>
        <th>Item count</th>
        <th>Stat 1</th>
        <th>Stat 2</th>
        <th>Etc</th>
   </tr>
<%  var alternating = false;
    foreach(RowItem rowItem in Model.RowItems) 
    { %>
   <%= Html.BeginRow(alternating) %>
   <%= Html.ShowRow(rowItem) %>
   <% alternating = !alternating; %>
   <%= Html.EndRow() %>
   <% } %>
</table>

其中 BeginRowEndRowShowRow 是我编写的辅助扩展方法。 ShowRow 类似于

public static string ShowRow(this HtmlHelper html, RowItem rowItem)
{
   return "<tr><td>.... </tr>";
}

但我也可以将 Html.ShowRow(rowItem) 替换为另一个部分视图,即 Html.RenderPartial("RowControl", rowItem);< /code>

问题是,这两种方法的优点和缺点是什么?我能想到的是,部分视图更“可设计”——当 amrkup 很复杂时,并且由专门从事“设计”和“客户端开发”的团队成员(即 html、css 和 javascript 而不是 c#)使用,更容易使用。 HTML 帮助器方法只是组装一个包含所需 html 的字符串,这对老派编码人员很有吸引力。

哪一项(如果有的话)对数百个项目有明显的性能影响?它们对可维护性有影响吗?在其他条件相同的情况下,您会选择使用哪一个?

我们使用的是 Asp.Net MVC 1.0 和 Visual Studio 2008,但可能会在未来几个月内进行升级。

We have a small Asp.Net MVC project which displays a report of several tens or hundreds of rows of data. Each row is represented by an object on the view models.

Currently we are showing the rows like this:

<table>
   <tr style="text-align:center">
        <th>Name</th>
        <th>Item count</th>
        <th>Stat 1</th>
        <th>Stat 2</th>
        <th>Etc</th>
   </tr>
<%  var alternating = false;
    foreach(RowItem rowItem in Model.RowItems) 
    { %>
   <%= Html.BeginRow(alternating) %>
   <%= Html.ShowRow(rowItem) %>
   <% alternating = !alternating; %>
   <%= Html.EndRow() %>
   <% } %>
</table>

Where BeginRow, EndRow and ShowRow are helper extension methods that I wrote. ShowRow is something like

public static string ShowRow(this HtmlHelper html, RowItem rowItem)
{
   return "<tr><td>.... </tr>";
}

But I could also replace Html.ShowRow(rowItem) with another partial view, i.e. Html.RenderPartial("RowControl", rowItem);

The question is, what are the advantages and drawbacks of these two approaches? All I can think of is that the partial view is more "designable" - easier to work with when the amrkup is complex, and by team members who specialise in "design" and "client development", i.e. html, css and javascript not c#. The HTML helper method is a just assembling a string containing the required html, which appeals to an old-school coder.

Which one, if any, has noticeable performance implications over hundreds of items? Are there maintenability impacts for either of them? All other things being equal, which would you choose to use?

We are on Asp.Net MVC 1.0 and Visual Studio 2008, but will probably upgrade some time in the next few months.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-09-15 08:53:24

我不确定这有多众所周知(我只是通过自己玩弄发现的),但您实际上可以使用匿名方法来定义页面内容的块,因此这是定义单独的部分视图的替代方法。我发现当我只想重用一小块 HTML/服务器代码时,或者如果我不想在部分视图和调用视图之间定义复杂的模型/视图数据契约时,这特别有用。

下面是嵌入在视图中的一些代码的示例:

// define example data
IEnumerable<MyDataItem> data = new MyDataItem[] {
    new MyDataItem { Id = 1, Value = "ABC" },
    new MyDataItem { Id = 1, Value = "BCD" },
    new MyDataItem { Id = 1, Value = "CDE" },
    new MyDataItem { Id = 1, Value = "DEF" },
};

// an anonymous method to define reused HTML/server code
Action<MyDataItem> displayData = delegate(MyDataItem item)
{
    %><span class="my-data-class" title="This is my data!"><%= item.Value %></span><%   
};
%><div><%

// use the displayData delegate to render a table       
%><table><%
foreach (MyDataItem item in data)
{
    %><tr><td><% displayData(item); %></td></tr><%
}   
%></table><%

// use the displayData delegate to render a set of divs
foreach (MyDataItem item in data)
{
    %><div><% displayData(item);%></div><%
}   
%></div><%

上面的示例呈现以下 HTML(为了清晰起见,进行了格式化):

<div>
  <table>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        ABC</span>
      </td>
    </tr>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        BCD</span>
      </td>
    </tr>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        CDE</span>
      </td>
    </tr>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        DEF</span>
      </td>
    </tr>
  </table>
  <div>
    <span class="my-data-class" title="This is my data!">ABC</span>
  </div>
  <div>
    <span class="my-data-class" title="This is my data!">BCD</span>
  </div>
  <div>
    <span class="my-data-class" title="This is my data!">CDE</span>
  </div>
  <div>
    <span class="my-data-class" title="This is my data!">DEF</span>
  </div>
</div>

I'm not sure how well known this is (I just found out by playing around myself), but you can actually use anonymous methods to define chunks of your page content, so that is an alternative to defining a separate partial view. I find this especially useful when just have a small chunk of HTML/server code that I want to reuse, or if I don't want to define a complex Model/ViewData contract between a partial view and the calling view.

Below is an example of some code embedded in a view:

// define example data
IEnumerable<MyDataItem> data = new MyDataItem[] {
    new MyDataItem { Id = 1, Value = "ABC" },
    new MyDataItem { Id = 1, Value = "BCD" },
    new MyDataItem { Id = 1, Value = "CDE" },
    new MyDataItem { Id = 1, Value = "DEF" },
};

// an anonymous method to define reused HTML/server code
Action<MyDataItem> displayData = delegate(MyDataItem item)
{
    %><span class="my-data-class" title="This is my data!"><%= item.Value %></span><%   
};
%><div><%

// use the displayData delegate to render a table       
%><table><%
foreach (MyDataItem item in data)
{
    %><tr><td><% displayData(item); %></td></tr><%
}   
%></table><%

// use the displayData delegate to render a set of divs
foreach (MyDataItem item in data)
{
    %><div><% displayData(item);%></div><%
}   
%></div><%

The above example renders the following HTML (formatted for clarity):

<div>
  <table>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        ABC</span>
      </td>
    </tr>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        BCD</span>
      </td>
    </tr>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        CDE</span>
      </td>
    </tr>
    <tr>
      <td>
        <span class="my-data-class" title="This is my data!">
        DEF</span>
      </td>
    </tr>
  </table>
  <div>
    <span class="my-data-class" title="This is my data!">ABC</span>
  </div>
  <div>
    <span class="my-data-class" title="This is my data!">BCD</span>
  </div>
  <div>
    <span class="my-data-class" title="This is my data!">CDE</span>
  </div>
  <div>
    <span class="my-data-class" title="This is my data!">DEF</span>
  </div>
</div>
素衣风尘叹 2024-09-15 08:53:24

在为 MVC 项目创建视图时,我通常会尝试将所有相同的元素生成保留在同一视图中。因此,对于您的情况,由于您正在创建一个表,我将确保所有表标签都是在同一视图内创建的。这有助于确保您创建格式良好的 HTML,并使跟踪验证问题变得更加容易。

由于几个原因,我不太喜欢这里发生的事情。显示行扩展可能采用某种自定义 HtmlTableRow 对象作为其输入,该对象中可能包含一组 HtmlTableCell 对象,每个对象都设置了样式属性它。由于这些 HtmlTableRow 是在您的控制器中创建的,因此它将您的演示文稿与控制器紧密耦合,这违背了 MVC 的关注点分离原则。

接下来,如果您将 ShowRow 替换为渲染部分,它将不会遵循我在第一段中建议的准则,即在不同视图中渲染同一元素的标签。有时,您可以在不同的函数中渲染元素,但如果这样做,它们返回的对象应该实现一次性模式,并且只能使用 using 语句来实现。

如果我要对此进行修改,以便设计人员可以更好地控制设计,我将在一个部分视图中呈现所有表格,并且为每个单元格指定一个描述性类名称。我还会将整个表格放入部分视图中,即使它是此页面上唯一的内容。

When creating views for MVC projects, I generally try to keep all of the same element generation in the same view. So for your case, since you are creating a table, I would make sure that all table tags are created inside the same view. This helps ensure that you are creating well formed HTML and makes tracking down validation issues much easier.

I don't really like what is going on here so much for a couple of reasons. The show row extension is likely taking some kind of custom HtmlTableRow object as its input, which likely has a collection of HtmlTableCell objects in it, each of which has stylistic properties set on it. Since these HtmlTableRows are created in your controller, it tightly couples your presentation to your controller which defeats the separation of concerns principle of MVC.

Next, if you replace ShowRow with a render partial, it will not follow the guideline I suggest in the first paragraph by rendering tags for the same element in different views. Occasionally you can get away with rendering your elements in different functions, but if you do so, the objects they return should implement the disposable pattern and only be implemented with using statements.

If I were going to modify this so that designers could have more control over the design, I would render the table all in one partial view, and I would give every cell a descriptive class name. I would also take the entire table and put it in a partial view, even if it were the only thing on this page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文