ASP.NET MVC视图 - 我可以使用代码隐藏

时间:2022-02-19 15:25:29

I am new to MVC and I notice that the view pages can look pretty nasty with all the intermixed script tags. Does it make sense to generate the HTML in a method in the view code-behind and then just insert the string result of the method as a single bit of script?

我是MVC的新手,我注意到所有混合脚本标签的视图页面看起来都很糟糕。在视图代码隐藏中的方法中生成HTML然后只是将方法的字符串结果作为单个脚本插入是否有意义?

For example: <div><%= GenerateTonsOfHTMLFromSomeIEnumerable() %></div>

例如:

<%= GenerateTonsOfHTMLFromSomeIEnumerable()%>

Is this contrary to the MVC philosophy? Dumb for some other reason, like performance? Does it have any merit?

这与MVC理念相反吗?愚蠢的其他一些原因,如表现?它有什么优点吗?

4 个解决方案

#1


10  

It's a matter of practice.

这是一个实践问题。

Here's an interesting read.

这是一个有趣的读物。

  1. Codebehind files are evil

    Codebehind文件很邪恶

  2. Codebehind files are not evil

    Codebehind文件并不邪恶

Also there's an interesting article by "Rob Conery"

还有一篇有趣的文章“Rob Conery”

  1. Inline Scripting and Code behind
  2. 内联脚本和代码背后

So, the choice is yours. It depends on your application architecuture, how you want to structure it, blah-blah...

所以,选择是你的。这取决于你的应用程序架构,你想如何构建它,等等......

I guess the advantage with views without codebehind is that is is much easier to switch to different viewengine which doesn't support codebehind.

我想没有代码隐藏的视图的优点是更容易切换到不支持代码隐藏的不同viewengine。

Though there will be some amount of work, but still it will be much seamless.

虽然会有一些工作量,但仍然会很无缝。

#2


1  

OK, chalk up another for the learning curve. I think I might have found the answer to my own question...

好的,为学习曲线写下另一个。我想我可能已经找到了自己问题的答案......

ASP MVC lets you install your own custom view engines which you can use (instead of the default ASP.NET view engine). So you can control the html generation. Once you know what to search for, its easy :)

ASP MVC允许您安装自己可以使用的自定义视图引擎(而不是默认的ASP.NET视图引擎)。所以你可以控制html的生成。一旦你知道要搜索什么,它很容易:)

Here's a good place to get started: Custom View Engine Example

这是一个入门的好地方:自定义视图引擎示例

@Rajesh - Thanks for the great links!

@Rajesh - 感谢您的精彩链接!

#3


1  

I wouldn't. You just need to forget about the codebehind file, seriously. In fact in MVC Framework RC1 by default there is no codebehind file for your views. Whatever c# processing you want to go on to render your view you can do it inline with your HTML. If you are thinking about doing some data processing, do yourself a favor and put it in the controller.

我不会。您只需要严肃地忘记代码隐藏文件。事实上,在MVC框架RC1中,默认情况下,您的视图没有代码隐藏文件。无论您想要继续渲染视图的c#处理,都可以使用HTML内联。如果您正在考虑进行一些数据处理,请帮个忙,并将其放入控制器中。

This is a paradigm shift, its best to just break your old habits now.

这是一种范式转变,现在最好打破你的旧习惯。

#4


0  

You can use code behind as well. It is only a matter of style preference.

您也可以使用后面的代码。这只是风格偏好的问题。

#1


10  

It's a matter of practice.

这是一个实践问题。

Here's an interesting read.

这是一个有趣的读物。

  1. Codebehind files are evil

    Codebehind文件很邪恶

  2. Codebehind files are not evil

    Codebehind文件并不邪恶

Also there's an interesting article by "Rob Conery"

还有一篇有趣的文章“Rob Conery”

  1. Inline Scripting and Code behind
  2. 内联脚本和代码背后

So, the choice is yours. It depends on your application architecuture, how you want to structure it, blah-blah...

所以,选择是你的。这取决于你的应用程序架构,你想如何构建它,等等......

I guess the advantage with views without codebehind is that is is much easier to switch to different viewengine which doesn't support codebehind.

我想没有代码隐藏的视图的优点是更容易切换到不支持代码隐藏的不同viewengine。

Though there will be some amount of work, but still it will be much seamless.

虽然会有一些工作量,但仍然会很无缝。

#2


1  

OK, chalk up another for the learning curve. I think I might have found the answer to my own question...

好的,为学习曲线写下另一个。我想我可能已经找到了自己问题的答案......

ASP MVC lets you install your own custom view engines which you can use (instead of the default ASP.NET view engine). So you can control the html generation. Once you know what to search for, its easy :)

ASP MVC允许您安装自己可以使用的自定义视图引擎(而不是默认的ASP.NET视图引擎)。所以你可以控制html的生成。一旦你知道要搜索什么,它很容易:)

Here's a good place to get started: Custom View Engine Example

这是一个入门的好地方:自定义视图引擎示例

@Rajesh - Thanks for the great links!

@Rajesh - 感谢您的精彩链接!

#3


1  

I wouldn't. You just need to forget about the codebehind file, seriously. In fact in MVC Framework RC1 by default there is no codebehind file for your views. Whatever c# processing you want to go on to render your view you can do it inline with your HTML. If you are thinking about doing some data processing, do yourself a favor and put it in the controller.

我不会。您只需要严肃地忘记代码隐藏文件。事实上,在MVC框架RC1中,默认情况下,您的视图没有代码隐藏文件。无论您想要继续渲染视图的c#处理,都可以使用HTML内联。如果您正在考虑进行一些数据处理,请帮个忙,并将其放入控制器中。

This is a paradigm shift, its best to just break your old habits now.

这是一种范式转变,现在最好打破你的旧习惯。

#4


0  

You can use code behind as well. It is only a matter of style preference.

您也可以使用后面的代码。这只是风格偏好的问题。