简体   繁体   English

在部分视图控制器中无法正常工作。(错误 - 不允许子操作执行重定向操作。)

[英]doesn't work redirecttoaction in partial view controller.(Error - Child actions are not allowed to perform redirect actions.)

Child actions are not allowed to perform redirect actions. 不允许子操作执行重定向操作。 Where do I need to set code? 我在哪里需要设置代码?

I've tried redirecttoaction in another controller. 我在另一个控制器中尝试过redirecttoaction。 It works. 有用。 But it does not work in menucontroller. 但它在menucontroller中不起作用。 That is my problem. 那是我的问题。 Please show me right direction. 请告诉我正确的方向。

 ```In layout inside of share folder. 
    <body id="backGroundColor"> 
    <div class="row">
        <div class="col-lg-12" style="text-align:center;background-color:#e6ffff;color:#007bff;font-weight:bold; font-size:30px;font-family:'Times New Roman', Times, serif">
            <div class="col-lg-1">
                <img src="~/wwwroot/Image/bfe.png" alt="logo" style="width:180px;height:70px;font-weight:bold;">
            </div>
            <div class="col-lg-11">
                Management System
            </div>
        </div>
        <div class="col-lg-12">
            <nav class="navbar navbar-inverse navbar-fixed" id="navmenu" style="width:100%">
                <div class="container-fluid">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                    </div>
                    <div class="collapse navbar-collapse navbar-fixed-Middle" id="myNavbar" >
                        @Html.Action("MenuLayout", "Menu")
                    </div>
                </div>
            </nav>
        </div>
    </div>
<br /><br />
        <div class="container-fluid ">
            <div>
                @RenderBody()
            </div>
        </div><br />
        <footer id="forFooter" style="z-index: 10;">
                <p>&copy; @System.DateTime.Now.Year -Copyright WMS 8.0.0</p>
        </footer>

    ```In MenuController
      public ActionResult MenuLayout()
            {
                if (cRoldId == null)
                {
                    return RedirectToAction("Create", "Issue");
                }
    }
  //Instead of this, 
  @Html.Action("MenuLayout", "Menu")


  //you should use 
  @Html.Partial("partial1", "Controller1")

  //You should create two partial _MenuPartial and CreatePartial



  //In your Shared/Layout
  @if(cRoldId == null)//or any other condition depending of your variable
  {
    @Html.Partial("MenuLayout", "MenuController")
  }
  else
  {
    @Html.Partial("Create", "Issue")
  }

If you need to do this after loading page or any other transaction, you can use ajax calls : 如果您在加载页面或任何其他事务后需要执行此操作,则可以使用ajax调用:

in your menu controller : 在您的菜单控制器中:

        public ActionResult MenuLayout(string cRoldId)
        {
            if (cRoldId == null)
            {
                return PartialView("Create"); //Create must be in the controllers folder.
            }
            else 
            {
               return PartialView("MenuLayout"); //menu layout must be a partial view.
            }

         }


 <script type="text/javascript">

  //Executes on load event of the document
  $(document).load(function(){

function printProjectFiles() {

    $.ajax({
        url: '@Url.Action("MenuLayout", "Menu")',//MenuLayout is your method name, Menu is your controller name
        type: 'GET',
        data: {cRoldId :"yourParamValue"},
        dataType: 'HTML',
        success: function (result) {
            $("#myNavbar").html(result);//returns a partial view and inserts it into your control.
        },
        error: function (err) {

        }
    });
}

  });

</script>

Tell me if it works. 告诉我它是否有效。

Instead of @Html.Action("MenuLayout", "Menu") in your view, 而不是@Html.Action("MenuLayout", "Menu")在您的视图中,

try using @Url.Action("MenuLayout", "Menu") 尝试使用@Url.Action("MenuLayout", "Menu")

It had worked for me for a similar scenario I had faced some time ago. 对我来说,这对我前段时间遇到的类似情况起了作用。

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

相关问题 子操作不允许执行重定向操作。 (使用PartialViews) - Child actions are not allowed to perform redirect actions. (Using PartialViews) 错误:子操作不允许执行重定向操作 - Error: Child actions are not allowed to perform redirect actions 子操作不允许执行重定向操作错误 - Child actions are not allowed to perform redirect actions error “不允许子操作执行重定向操作” - “Child actions are not allowed to perform redirect actions” 子操作不允许执行重定向操作 - Child actions are not allowed to perform redirect actions 子操作不允许执行重定向操作MVC4 - Child actions are not allowed to perform redirect actions MVC4 ASP.NET MVC 3 + Razor错误:不允许子操作执行重定向操作 - ASP.NET MVC 3 + Razor Error: Child actions are not allowed to perform redirect actions 不允许子操作执行重定向操作 - 使用ASP.NET MVC 2和Razor时出错 - Child actions are not allowed to perform redirect actions - error while working with ASP.NET MVC 2 and Razor 如何解决不允许子操作执行重定向操作,其他答案不解决 - How to fix Child actions are not allowed to perform redirect actions, other answers does not fix 一个类中的2个线程必须执行不同的操作。 我的代码不起作用 - 2 threads in a class have to perform different actions. My code is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM