简体   繁体   English

ASP.NET MVC3视图组合+部分视图冲突

[英]ASP.NET MVC3 Combination of Views + Partial Views Conflicting

I have the following structure: 我有以下结构:

Views: 浏览次数:

Home

Articles 用品

Partial Views: 部分观点:

Book

Classes

Intro 介绍

Faq 常问问题

On the top of the page, in my header, is a navigation menu - containing multiple Ajax.ActionLinks that call into different methods in my HomeController class to fetch a particular partial view and then place that content in my center DIV. 在页面的顶部,在我的标题中,是一个导航菜单 - 包含多个Ajax.ActionLinks,它们调用我的HomeController类中的不同方法来获取特定的局部视图,然后将该内容放在我的中心DIV中。 So I can click on the Faq link, and the content in the center DIV of my page will now contain the partial view for my Faq. 所以我可以点击Faq链接,我页面中心DIV中的内容现在将包含我的常见问题解答的局部视图。 Now, I have an Articles view that will look completely different and therefore needs to be separated as it's own view as opposed to a partial view, and, the problem that I am running into is if I click on my Html.ActionLink for "Articles", I am navigated to my Articles view, which is what I want, but then if I click on one of the other links that are Ajax.ActionLinks, I need (somehow) the view to change back to my Home view so that I can then retrieve the requested partial view and place that into my center DIV. 现在,我有一个看起来完全不同的文章视图,因此需要分开它自己的视图而不是局部视图,而我遇到的问题是如果我点击我的Html.ActionLink为“文章” “,我导航到我的文章视图,这是我想要的,但是如果我点击Ajax.ActionLinks的其他链接之一,我需要(不知何故)视图更改回我的主视图,以便我然后可以检索请求的局部视图并将其放入我的中心DIV。 Any idea how I can do this? 知道我怎么能这样做吗? Right now the AJAX.ActionLink, when being called while on my Articles View, is calling into my HomeController, but then trying to replace the content in my center DIV (which doesn't exist in my Articles view), and additionally, it's not loading the HomeView. 现在AJAX.ActionLink在我的文章视图中被调用时,正在调用我的HomeController,但后来尝试替换我的中心DIV中的内容(我的文章视图中不存在),此外,它不是加载HomeView。

Any thoughts on how to best achieve this? 有关如何最好地实现这一点的任何想法? Also, I am not using the Razor engine. 另外,我没有使用Razor引擎。

So, the navigational menu, which is a set of links, resides in my Site.Master file. 因此,导航菜单(一组链接)驻留在我的Site.Master文件中。 Here is one of the links that I have: 这是我的链接之一:

  <%= Ajax.ActionLink("Book", "LoadBook", new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "content", InsertionMode = InsertionMode.Replace })%>

When clicking on this link, the method LoadBook in my HomeController is called. 单击此链接时,将调用HomeController中的LoadBook方法。 Refer to the code below: 请参阅以下代码:

public ActionResult LoadBook()
{
    return PartialView("Book");
}

In my Home.aspx view, I have the following code: 在我的Home.aspx视图中,我有以下代码:

<div id="content">
   <% Html.RenderPartial("Intro"); %>
</div>

This code will automatically load the partial view for my "intro" (introduction) by default whenever initially loading the Home view. 在最初加载Home视图时,默认情况下,此代码将自动加载我的“简介”(简介)的部分视图。 So basically, since the link for "Articles" is Html.ActionLink (as opposed to Ajax.ActionLink), when it is clicked I am taken to the Articles view. 所以基本上,因为“Articles”的链接是Html.ActionLink(而不是Ajax.ActionLink),当它被点击时,我将被带到Articles视图。 When I click on, for example, the "Book" Ajax.ActionLink in my Site.Master header while on the Articles view, the code will try to update a "content" DIV with the "Book" partial view. 例如,当我在Site视图中点击我的Site.Master标题中的“Book”Ajax.ActionLink时,代码将尝试使用“Book”局部视图更新“content”DIV。 This behavior is only wanted when on the Home view, for obvious reasons. 出于显而易见的原因,仅在Home视图上才需要此行为。 So, how can I have different behavior for my Ajax.ActionLinks when viewing a view that is different than my Home view? 那么,在查看与我的Home视图不同的视图时,如何为Ajax.ActionLinks提供不同的行为?

Thanks! 谢谢!

Chris 克里斯

This sort of architecture is going to give you lots of problems. 这种架构会给你带来很多问题。 For stanrtes, you can't link to any pages. 对于stanrtes,您无法链接到任何页面。 All your content happens in your main page. 您的所有内容都发生在您的主页面中。

This works ok for an app like GMail, but most people will be confused as to why they can't save a shortcut to a page, or send a link to a friend. 这适用于像GMail这样的应用程序,但大多数人会对为什么他们无法保存页面的快捷方式或向朋友发送链接感到困惑。

HTML is designed to have a unique URL for each page. HTML旨在为每个页面提供唯一的URL。 You're fighting upstream trying to do something different. 你在上游试图做一些与众不同的事情。 Suppose you want to use fragments to jump to other sections of your page. 假设您要使用片段跳转到页面的其他部分。 you can't do that. 你不能这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM