ASP.NET MVC:显示部分表的部分视图

发布于 2024-11-01 18:52:12 字数 466 浏览 1 评论 0原文

我有两个视图,每个视图都呈现一个表格。大多数表在两个视图之间共享,因此我希望能够为它们重用视图代码。

表1

Column A   |   Column B
1          |   2
4          |   8

表2

Column A   |   Column B   |   Column C
1          |   2          |   7
4          |   8          |   2

显然,我的实际表格比上面的示例复杂得多,这只会使问题更加复杂。由于表格是如何用 HTML 编写的,在我看来,似乎没有什么好方法可以删除我想要重用的内容,同时仍然能够添加内容。

使用表格,我可以采用任何方法来重用代码吗?还是我必须放弃使用表格?

I have two views that render a table on each. Most of the tables are shared between both views so I want to be able to reuse the view code for them both.

Table 1

Column A   |   Column B
1          |   2
4          |   8

Table 2

Column A   |   Column B   |   Column C
1          |   2          |   7
4          |   8          |   2

Obviously, my actual tables are a lot more complicated than the example above, which only serves to compound the problem. Because of how tables are written in HTML, there seems to me no good way to cut out the stuff I want to reuse while still having the ability to add to it.

Using tables, are there any methods I can employ to reuse the code or will I have to move away from using tables?

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

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

发布评论

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

评论(3

没有你我更好 2024-11-08 18:52:12

为什么不创建一个专门用于构建给定包含所有列和行信息的特定模型的表的视图呢?这样,您可以在任何您想要的地方使用视图,并且您提供的模型将确定有多少列和行。

编辑

我认为您不太理解我的观点,所以我将使用一些代码。现在您可能有一个看起来像这样的模型:

public class Something
{
    public string ValueA {get;set;}
    public string ValueB {get;set;}
    public string ValueC {get;set;}
}

您的视图可能看起来像这样:

<table><tr><th>Column A</th><th>Column B</th><th>Column C</th></tr>
    <%foreach(var something in Model.Somethings) {%>
       <tr>
           <td><%:something.ValueA%></td>
           <td><%:something.ValueB%></td>
           <td><%:something.ValueA%></td>
       </tr>
    <%}%>
</table>

嗯,正如您所注意到的,这并不适合简单的表列。一种解决方案是在模块上设置一些选项来确定包含哪些列,然后将一堆 if/then 语句放入视图中。但我认为更好的解决方案是创建一个看起来更像这样的新 Model 类:

public class TableModel
{
    public IEnumerable<string> Headers {get;set;}
    public IEnumerable<IEnumerable<string>> Rows {get;set;}
}

然后您的模型看起来更像这样:

<table><tr>
    <%foreach(var header in Model.Headers) {%>
        <th><%:header%></th>
    <%}%></tr>
    <%foreach(var row in Model.Rows) {%>
       <tr>
       <%foreach(var cell in row) {%>
           <td><%:cell%></td>
        <%}%>
       </tr>
    <%}%>
</table>

正如您所看到的,此视图代码中没有任何内容将自身显式地与您的原始数据类型联系起来,因此它可以重复用于您想要在整个应用程序中创建的任何表。您的控制器负责根据您想要表示的数据创建 TableModel 对象。如果特定列包含在该模型中,则会显示该列。如果没有,就不会。

显然,就像您的示例一样,这非常简单,但它应该为您提供总体思路。 DataTable 类已经是 .NET 的一部分,可能已经拥有您需要的一切,因此您甚至不必创建自己的 TableModel 类。它已经提供了一些关于添加和删除列、行等的不错的功能。

Why not just create a view that specializes in building a table given a specific model that includes all the column and row information? That way, you can use the view anywhere you want, and the model you give it will determine how many columns and rows there are.

Edit

I don't think you're quite understanding my point, so I'll use some code. Right now you probably have a model that looks something like this:

public class Something
{
    public string ValueA {get;set;}
    public string ValueB {get;set;}
    public string ValueC {get;set;}
}

And your view probably looks something like this:

<table><tr><th>Column A</th><th>Column B</th><th>Column C</th></tr>
    <%foreach(var something in Model.Somethings) {%>
       <tr>
           <td><%:something.ValueA%></td>
           <td><%:something.ValueB%></td>
           <td><%:something.ValueA%></td>
       </tr>
    <%}%>
</table>

Well, as you have noticed, this doesn't lend itself well to simply columns from the table. One solution would be to set some options on the module to determine which columns get included, and then throw a bunch of if/then statements into your view. But I think a better solution would be to make a new Model class that looks more like this:

public class TableModel
{
    public IEnumerable<string> Headers {get;set;}
    public IEnumerable<IEnumerable<string>> Rows {get;set;}
}

Then your model looks more like this:

<table><tr>
    <%foreach(var header in Model.Headers) {%>
        <th><%:header%></th>
    <%}%></tr>
    <%foreach(var row in Model.Rows) {%>
       <tr>
       <%foreach(var cell in row) {%>
           <td><%:cell%></td>
        <%}%>
       </tr>
    <%}%>
</table>

As you can see, there is nothing in this view code that ties itself explicitly to your original data type, so it can be reused for any table you want to create throughout your entire application. Your controller becomes responsible for creating a TableModel object from the data that you want to represent. If a particular column gets included in that model, it will be displayed. If not, it won't.

Obviously, like your example, this is extremely simplified, but it should give you the general idea. The DataTable class, which is already part of .NET, might already have everything you need, so you won't even have to create your own TableModel class. It's already got some nice functionality around adding and removing columns, rows, etc.

简美 2024-11-08 18:52:12

我建议为通用代码创建一个 HTMLHelper。我会将条件传递给助手以确定要输出的内容。

I would suggest creating a HTMLHelper for the common code. I would pass the condition to the helper to determine what to output.

ま昔日黯然 2024-11-08 18:52:12

尝试分两步思考问题。一个是生成标题并获取所需的列,另一个是实际渲染 Html。然后你可以调用像 Which 这样的方法

RenderTable( string[] columnsINeed )

来渲染你需要的表列。

您需要呈现 HTML 的方式不应妨碍您编写可重用的代码。

Try thinking of the problem in two steps. One is generating the headers and grabbing the columns that you need, another is actually rendering the Html. Then you can call a method like

RenderTable( string[] columnsINeed )

Which will render the table columns you need.

The way you need to render HTML shouldn't prevent you from writing re-usable code.

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