任何避免 C# 和 javascript 中重复代码的想法

发布于 2024-08-18 02:42:44 字数 258 浏览 5 评论 0原文

我有一个 asp.net mvc 网站,我使用 C# 构建了大部分页面,例如根据视图模型中的一组数据构建 html 表,

我还有很多 javascript,然后动态修改这些表(例如添加行) )。

添加新行的 javascript 代码看起来与我在 C# 中的“渲染”代码非常相似,该代码首先用于构建 html 表。

每次我更改 C# 代码以添加新字段时,我都必须记住返回 JavaScript 代码来执行相同的操作。

这里有更好的方法吗?

i have an asp.net mvc website where i build up most of the page using C# for example building up html tables given a set of data from my viewmodel

i also have a lot of javascript that then dynamcially modifies these tables (add row for example).

the javascript code to add a new row looks extremely similar to my "rendering" code i have in C# that is used to build up the html table in the first place.

Every time i change the c# code to a add a new field, i have to remember to go back to the javascript code to do the same.

is there a better way here?

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

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

发布评论

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

评论(3

小耗子 2024-08-25 02:42:44

实现此目的的一种方法是将生成标记的逻辑公开为服务器上的 Web 服务,并让 JavaScript 通过 AJAX 调用获取该标记,而不是重复逻辑。

通过这种方式,您可以从 JavaScript 调用类似 CreateRow 方法的方法,该方法实际上会调用与呈现页面时在服务器上使用的完全相同的逻辑。

One way to do this is to expose the logic that generates the markup as a web service on your server and have JavaScript get that markup via an AJAX call rather than duplicating the logic.

This way you can call something like a CreateRow method from JavaScript that will actually invoke the same exact logic that you used on the server when you rendered the page.

锦爱 2024-08-25 02:42:44

看看Scott Gu的文章: ASP.NET MVC 2:模型验证

基本上,您在模型属性级别定义验证,并且 ASP.NET MVC 2 也可以自动生成正确的客户端验证。

不幸的是,这可能意味着您必须将所有内容重构到 MVC 中,但这里的很多人可能会认为这是一个优点。

(免责声明:我根本没有使用过 ASP.NET MVC)

Look at Scott Gu's article: ASP.NET MVC 2: Model Validation

Basically, you define the validations at the model property level, and ASP.NET MVC 2 can automatically generate the proper client-side validation as well.

Unfortunately, this may mean you'll have to refactor everything into MVC, but a lot of ppl here would probably view that as a plus.

(disclaimer: I have not worked with ASP.NET MVC at all)

淡看悲欢离合 2024-08-25 02:42:44

与@Andrew Hare 所接受和流行的答案相反。我宁愿仅在 javascript 中实现标记生成(并重用它来构建 HTML 表),也不愿仅在 C# 中实现标记(当然,如果 javascript 也需要该功能)。

因为:

  1. 对于 HTML 来说,Javascript 比 C# 更好,因为它可以修改已经渲染的 DOM。因此,使用 Javascript,您可以做的不仅仅是静态 HTML。也许您现在不需要它,但如今,旨在使应用程序变得更像富互联网应用程序的要求并不罕见。
  2. AJAX 调用很可能比最糟糕的 Javascript 实现慢,后者处理服务器端数据并将其转换为现有表的新行。而且比用 Javascript 从头开始​​制作表格还要慢。
  3. 更少的服务器请求(以及更多的客户端要求,但这通常不是问题)。
  4. 有几个非常好的 Javascript 框架可以制作非常漂亮的表格(以及更多)。

我知道 ASP.NET MVC 有很多关于如何将 HTML Helpers 用于控件的很好的示例,从而实现干净、快速的开发。但是,如果您最终还是创建了 Javascript 函数,那么我认为您应该再次考虑关注点分离,并坚持使用 Javascript 并放弃 HTML 帮助程序中的重复内容。

因为我想,如果您已经创建了行,那么扩展 Javascript 函数来创建整个表应该不会那么难。以下是如何将数据传递给 Javascript,然后使用 Javascript 中已有的函数“createRow”的想法:

<script>
    var data = [];
    <% foreach (var item in Model) { %>
        data.push({
            Id: <%= Html.Encode(item.Id) %>
            , Title: <%= Html.Encode(item.Title) %>
        });
    <% } %> 
    createTableHeader();
    for (var i = 0; i < data.length; i++) {
        createRow(data[i]);
    }
    createTableFooter();
</script>

Contrary to accepted and popular answer by @Andrew Hare. I would rather implement the markup generation only in javascript (and reuse that for building HTML tables) than implement markup only in C# (if javascript needs that functionality too, of course).

Because:

  1. Javascript is is better than C# for HTML, because it can modify the already rendered DOM. So with Javascript you can do much more than just static HTML. And maybe you don't need it now, but nowadays are not rare the requirements that aim for the application becoming more like a Rich Internet Application.
  2. AJAX calls are more likely to be slower than the worst Javascript implementation which handles server-side data and transforms that into new rows of an existing table. And also slower than making the table from scratch in Javascript.
  3. Less server requests (and more client side requirements, but that usually is not a problem).
  4. There a several very good Javascript frameworks that makes really nice tables (and much more).

I know that ASP.NET MVC has a lot of nice examples on how to use HTML Helpers for your controls, resulting in clean and fast development. But if you end up creating Javascript functions anyway then I think you should consider again the separation of concerns, and stick with Javascript and discard the duplication in the HTML Helpers.

Because expanding your Javascript functions to create the whole table if you are already creating the rows should not be that hard I guess. Here is an idea of how to pass the data to Javascript and then using the function "createRow" that you already have in Javascript:

<script>
    var data = [];
    <% foreach (var item in Model) { %>
        data.push({
            Id: <%= Html.Encode(item.Id) %>
            , Title: <%= Html.Encode(item.Title) %>
        });
    <% } %> 
    createTableHeader();
    for (var i = 0; i < data.length; i++) {
        createRow(data[i]);
    }
    createTableFooter();
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文