1

我目前正在努力将旧页面更新为新的 Razor 语法,并希望我可以回收旧代码中已有的大部分代码隐藏函数。用asp标签,写类似的东西

<asp:Textbox ID="txtcust" runat="server">

将允许我在我的代码隐藏中访问 txtcust 作为变量,以便页面获取/设置文本框中的数据。我可以用新的 Razor 语法做一些同样简单的事情吗?

4

4 回答 4

3

您从根本上误解了 MVC。

MVC 使用模型,而不是控件。

于 2012-05-07T19:13:20.713 回答
1

You could use TextBoxFor and bind the textbox to the model (this is one of the perks of MVC and Razor syntax).

@Html.TextBoxFor(model => model.Property)

Then in your controller you can manipulate the model values so they are reflected in your View automatically. This is essentially the way you do it in MVC as opposed to stinky Web Forms.

Hope this helps!

于 2012-05-11T02:07:05.950 回答
0

尽管它们都建立在 ASP.NET 框架之上,但 MVC 和 WebForms 确实没有太多共同点。MVC 依赖于模型、视图和控制器,它们并没有真正(直接)转换为代码隐藏和服务器控件的 WebForms 结构。我的建议是查看 MVC 教程并从头开始构建您的站点,直到您完全了解如何将 WebForms 结构映射到 MVC。这并不简单,但需要一些时间和耐心才能完成。

http://www.asp.net/mvc/tutorials

于 2012-05-11T02:19:38.177 回答
0

如果您想使用帮助程序来创建 HTML 控件(请记住,您不会像在 WebForms 中那样在代码隐藏中与它们“交互”),您可以使用:

@Html.TextBox()
于 2012-05-07T19:16:10.010 回答