ASP.NET MVC 结构以及如何使用操作和控制器

发布于 2024-12-08 16:27:22 字数 220 浏览 3 评论 0原文

我正在开发一个 ASP.NET MVC 结构的应用程序。 我想知道,我正在尝试创建一个只有一个页面的网站,它的左侧有一个导航栏(这是用户输入的列表),然后是中间的主要内容。

我的问题是,我可以使导航栏(用户输入列表)静态,并根据用户输入通过异步 AJAX 回发更改主要内容吗?我将有多个控制器,它们会根据用户输入为主要内容设置不同的结果。

我将如何解决这个问题,部分视图和使用异步回发调用操作?

I am developing an application in ASP.NET MVC structure.
I was wondering, I am trying to create a site that only has one page, it has a navigation bar on the left (Which is a list of user input), and then the main content in the middle.

My question is, can I make the navigation bar (The user input list) static, and make the main content change, based on the user input, with an asynchronous AJAX postback? I would have multiple controllers which would set different results to the main content, based on the user input.

How would I go around doing so, partial views and calling actions with asyncpostback's?

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

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

发布评论

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

评论(1

黄昏下泛黄的笔记 2024-12-15 16:27:22

是的,我会完全按照您的建议进行操作 - 具有呈现 PartialView([name]) 的操作,并可能使用 jQuery 的 ajax 来获取内容。

然后您可以使用 jQuery 将 html 注入到内容面板中。

有趣的是,如果您需要将 css 添加到页面的头部部分。

但是,通过勤奋地使用单个 CSS 或至少一组 CSS 文件,您可以避免这种情况。

但请注意,如果动态注入的内容具有表单,它会使表单回发变得复杂。您需要劫持表单提交过程并将其转换为另一个 ajax 调用,以将生成的 html 放入您的内容面板中 - 您甚至可能需要手动启动表单提交,因为我认为您无法将表单提交作为阿贾克斯操作。

我对命名 iframe 做了类似的事情(不一定需要脚本),因为您可以使用 target 渲染一个 form ,它等于一个框架;这意味着 iframe 内容可以是完整视图而不是部分视图(因此可以完全控制自己的脚本和 css)。当然,您会遇到在主机页面和该框架之间共享数据的问题。

Yes, I would do exactly as you have suggested - have actions that render PartialView([name]) and use, probably, jQuery's ajax to fetch the content.

You can then use jQuery to inject the html into the content panel.

Where it might get interesting is if you need to have css added to the head section of the page.

But then, with diligent use of a single CSS, or at least a single group of CSS files, you can sidestep that.

Be aware though, that it does complicate form post-backs if the dynamically injected content has forms. You would need to hijack the form submit process and turn it into another ajax call to get the resulting html into your content panel - you might even need to hand-crank the form submission because I don't think you can trigger form submission as an Ajax operation.

I've done a similar thing with named iframes (doesn't necessarily need script), as you can render a form with a target which is equal to the name of an IFrame; and it means that the iframe content can be a full view instead of a partial (and therefore has full control over its own script and css). Of course, then you have issues with sharing data between the host page and that frame.

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