如何在code隐藏页添加到一个视图或局部视图 [英] how to add a code-behind page to a view or partial view

查看:111
本文介绍了如何在code隐藏页添加到一个视图或局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC的最新版本注意到一个视图不再默认为有code-背后类。

I notice with the latest version of ASP.NET MVC that a View no longer defaults to having code-behind classes.

我该如何去增加一个code隐藏类现在视图或局部视图??

How do I go about adding a code-behind class now to a View or Partial View??

推荐答案

如何到code隐藏页添加到一个局部视图

这似乎不是特别靠谱,而且是相当DO-能。这个答案工作的偏 ViewUserControl ,但同样要申请一个普通MVC 的ViewPage 以及

Seems this wasn't particularly tricky, and is quite do-able. This answer worked for a Partial ViewUserControl but the same should apply for a Normal MVC ViewPage as well


  1. 添加一个新的类文件,&LT的约定;查看文件名和放大器;延伸>的.cs (即 view.ascx.cs

添加使用System.Web.Mvc; 的类

更​​改类从 ViewUserControl&LT来继承;>

公共类Foo:ViewUserControl

以下内容添加到视图的标题:

Add the following to the View's header:

codeBehind =View.ascx.cs继承=Project.Views.Shared.View

将文件复制出来的解决方案,并拖回两人一起重新关联。这可能不是在VS必要的2010+和MVC 2 +

Copy the files out of the solution and drag back in to re-associate the two together. This may not be necessary in VS 2010+ and MVC 2+.

有关这与正常的MVC视图中工作,你只需要从的ViewPage

For this to work with a normal MVC View, you just need to inherit the class from "ViewPage"

这篇关于如何在code隐藏页添加到一个视图或局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆