任何避免 C# 和 javascript 中重复代码的想法
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现此目的的一种方法是将生成标记的逻辑公开为服务器上的 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.看看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)
与@Andrew Hare 所接受和流行的答案相反。我宁愿仅在 javascript 中实现标记生成(并重用它来构建 HTML 表),也不愿仅在 C# 中实现标记(当然,如果 javascript 也需要该功能)。
因为:
我知道 ASP.NET MVC 有很多关于如何将 HTML Helpers 用于控件的很好的示例,从而实现干净、快速的开发。但是,如果您最终还是创建了 Javascript 函数,那么我认为您应该再次考虑关注点分离,并坚持使用 Javascript 并放弃 HTML 帮助程序中的重复内容。
因为我想,如果您已经创建了行,那么扩展 Javascript 函数来创建整个表应该不会那么难。以下是如何将数据传递给 Javascript,然后使用 Javascript 中已有的函数“createRow”的想法:
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:
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: