从 MVC3 后面的代码访问 html 标签

发布于 2024-12-26 10:00:03 字数 1053 浏览 3 评论 0原文

我正在尝试合并 Visual Studios 2008 中的两个项目

我试图引入的项目的样式类似于旧式 asp.net 项目,使用设置背后的代码来访问页面加载方法等,但其他项目是新奇的 MVC3 风格项目。

现在我完全意识到这两个项目的访问映射不同,很可能不应该放在一起,但如果我听了我被告知不要做的每件事,我就不会有天使的坏屁股纹身在我的左屁股上吃热狗。

现在,当我谈论将这两者合并在一起时,我真正谈论的是将 1 个 .aspx 类型的视图引入到我的 MVC3 项目中,并使其与其他视图一起运行。我已经能够将它引入,我在控制器设置中有一个函数,并且我将其后面的代码正确链接到了那里,一切似乎都很顺利,但智能感知无法识别具有它们的 id 和 runat 服务器属性将成为我后面代码中的对象。它一直告诉我声明这些变量。

对于狗屎和咯咯笑,我确实尝试声明它们,此时智能感知微笑着给我眨了眨眼,然后提醒我在解析页面时变量已经全部声明完毕,从而使我的希望破灭。

编辑:控制器正在执行它自己的任务,正如我上面所说,后面的代码正在处理页面加载功能,并且在旧的设置中,您可以直接访问使用 runat 服务器属性的任何 html 标签,我试图将其与 MVC3 一起复制,我不想将其全部放入控制器中。因此,为了澄清这一点,我有一个模型、视图、控制器,然后在我的视图中我有我的 aspx 页面,其代码页名为 filename.aspx.cs,这有帮助吗?

编辑2:对于这个设置来说,就像我上面所说的那样,使用的是vs2008,因此它具有视图模型和控制器文件夹,您可以在其中放置三个单独的区域,我有一个来自使用旧代码的另一个项目的特定视图使用 CodeBehind="Default.aspx.cs" 参数将代码文件附加到 .aspx 文件的背后方法,因此我从旧项目中提取了这个 .aspx 文件及其代码隐藏文件,并将其粘贴到新项目中MVC3 项目并告诉他们要共存,就像成年人告诉孩子不要把东西放进嘴里一样(只是因为你告诉他们并不意味着就会那样)。但文件后面的旧代码使用页面加载函数,该函数必须从某个库初始化一些 html 标签,但我当然无法访问这些标签,并且它们没有 html 帮助程序或任何类似的酷东西。

任何帮助将不胜感激。

I'm attempting to merge two projects in visual studios 2008

The project I am trying to bring in is styled like the old school asp.net projects using the code behind setups to access the on page load methods and so on, but the other projects is the new fangled MVC3 styled project.

Now I am fully aware that these two projects are mapped differently for access and most likely shouldn't be put together, but if i listened to every thing i was told not to do, i wouldn't have the bad ass tattoo of an angel eating a hot dog on my left ass cheek.

Now when I talk about merging these two together all I'm really talking about is bring in 1 view of .aspx type into my MVC3 project and making it run along side the other views that are there. I have been able to bring it in, I have a function in a controller setup for it and I have it's code behind there linked in properly, every thing seems to be a go, but intellisense doesn't recognize that the html tags that have an id and a runat server property on them are going to be objects in my code behind. It keeps telling me to declare these variables.

For shits and giggles I did try declaring them, at which point intellisense smiled and gave me a wink before dashing my hopes against a sharp rock by alerting me that the variables had all ready been declared when the page was parsed.

Edit: The controller is performing it's own tasks, as I stated above the code behind is handling the page load function and in the old setup the you could directly access any html tags that used the runat server propertie, I'm trying to duplicate this along side MVC3, I'm not trying to put it all in the controller. So to clear it up, I have a Model, View, controller, then inside my view i have my aspx page with a code page named filename.aspx.cs, does that help?

Edit2: OK for this setup like i said above were using vs2008 so it has the view model and controller folders where you place your three separate areas I have a particular view from another project that was using the old code behind approach of attaching a code file to a .aspx file by using the CodeBehind="Default.aspx.cs" parameter, so I pulled this .aspx file with it's code behind file from the old project and stuck it into the new MVC3 project and told them to coexist much like a an adult tells a child not to stick something in there mouth(just cause you tell em to doesn't mean there gonna). but the old code behind file uses a page load function that has to initialize some html tags from a certain library but i of course can't access these tags, and they don't have html helpers or any thing cool like that.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

别靠近我心 2025-01-02 10:00:03

这不是 MVC 的工作方式。从控制器的角度来看,它对视图一无所知。它们之间没有双向通信,它们不是从单个基础派生(如 WebForms 那样)等。控制器操作执行一些逻辑,构建视图模型(可选),并发送该视图模型(可选)到视图。一条路,没有回头路。

因此,不要像这样:

TextBox1.Text = "foo";

您可以在控制器操作中执行类似的操作:

myViewModel.SomeTextField = "foo";

然后,在视图中,您可以访问视图模型上的该值:

@Html.TextBox("name", Model.SomeTextField)

控制器不应该,必须永远绑定到视图。根据设计,操作方法可以将控制权传递给您想要的任何视图。它们不像 WebForms 那样绑定到“页面”。

这是关注点分离、控制反转,所有这些都是好东西。控制器构建一个对象并将其发送到一个视图,任何视图,无论是哪一个。视图需要来自一个控制器的视图模型,任何控制器,无论是哪一个。

This isn't how MVC works. From the perspective of the controller, it knows nothing about the view. There isn't a bi-directional communication between them, they don't derive from a single base (like WebForms does), etc. The controller action performs some logic, builds a viewmodel (optionally), and sends that viewmodel (optionally) to the view. One way, no going back.

So instead of something like this:

TextBox1.Text = "foo";

You'd do something like this in the controller action:

myViewModel.SomeTextField = "foo";

Then, in the view, you'd access that value on the viewmodel:

@Html.TextBox("name", Model.SomeTextField)

The controller should never, must never be bound to a view. By design, action methods can pass control to any view you want. They're not bound to a "page" like in WebForms.

It's a separation of concerns, an inversion of control, all that good stuff. The controller builds an object and sends it to a view, any view, doesn't matter which one. The view expects a viewmodel from a controller, any controller, doesn't matter which one.

余生一个溪 2025-01-02 10:00:03

我找到了解决方案,访问这些标签所需要做的就是使用 FindControl() 函数,在文件后面的代码中,就这么简单,现在运行起来就像冠军一样。
所以我拥有的是一个带有控件的视图,

<button id="foo" runat="server">

当然我有一个具有该功能的控制器

public ActionResult Index()
{
    Return View();
}

的代码

protected void Page_Load(object sender, EventArgs e)
{
    ((Button)This.FindControl("foo")).DoSomething();    
}

,最后我有包含Easy peasy柠檬榨汁

。感谢大家的帮助

I found the solution, all i needed to do to access these tags was use the FindControl() function, in side the code behind file, it was that simple, the thing runs like a champ now.
So what i had was a view with a control like

<button id="foo" runat="server">

then of course i had a controller with the function

public ActionResult Index()
{
    Return View();
}

and lastly I have the code behind which contains

protected void Page_Load(object sender, EventArgs e)
{
    ((Button)This.FindControl("foo")).DoSomething();    
}

Easy peasy lemon squeezy.

thanks for the help any way guys

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文