MVC3剃刀返回当前视图 [英] MVC3 razor return current view

查看:84
本文介绍了MVC3剃刀返回当前视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的母版页 _layout 这时候,pressed,改变我的菜单的布局得到了ActionLink的。但我不希望我的网页的当前内容会改变。

I've got a actionlink in my master page _Layout That when pressed, changes the layout of my menu. But I don't want the current Content of my page to change.

我怎样才能做到这一点与MVC3剃刀的没有的使用JavaScript?

How can I accomplish this with MVC3 razor without using javascript?

我猜它会沿着线的东西:

I'm guessing it will be something along the lines of:


  • 按钮被点击回发发生

  • 设置一些值,改变菜单状态持续

  • 返回previous视图

特别是回归previous视图部分困扰我,可能有人解释如何做到这一点?

Especially the "return previous view" part perplexes me, could someone explain how to accomplish this?

推荐答案

在你的控制器覆盖OnActionExecuting:

In your controller override OnActionExecuting:

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.QueryString["MenuLayoutName"] != null && IsValidMenuLayoutName(filterContext.RequestContext.HttpContext.Request.QueryString["MenuLayoutName"] != null))
            ViewBag.MenuLayoutName = filterContext.RequestContext.HttpContext.Request.QueryString["MenuLayoutName"];
    }

在您的_Layout.cshtml当渲染菜单,看ViewBag.MenuLayoutName来决定使用哪个菜单。最有效的方法是简单地创建局部视图,以便您可以渲染菜单如下:

In your _Layout.cshtml when you render the menu, look at the ViewBag.MenuLayoutName to decide which menu to use. The most efficient way is to simply create partial views so you can render the menu as follows:

@{ Html.RenderPartial(ViewBag.MenuLayoutName); }

不过,请注意调用IsValidMenuLayoutName的!否则,人们可以把任何有效的局部视图的名字在那里,并把它呈现在哪里你希望你的菜单出现。

However, take note of the call to IsValidMenuLayoutName! Otherwise people could put the name of any valid partial view in there and get it rendered where you expect your menu to appear.

在要允许用户选择不同的菜单布局,改变你的链接页面指定布局时使用的名称链接。

In your links where you want to allow the user to select the various menu layouts, change your link to the page to specify the name of the layout to use.

<a href="/Index?MenuLayoutName=RedLayout">Use Red Menu</a>

这篇关于MVC3剃刀返回当前视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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