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

时间:2019-05-06 09:22:42

标签: c# asp.net-mvc

不允许子操作执行重定向操作。我需要在哪里设置代码?

我尝试在另一个控制器中进行redirecttoaction。有用。但是它在menucontroller中不起作用。那是我的问题。请告诉我正确的方向。

 ```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");
                }
    }

2 个答案:

答案 0 :(得分:0)

  //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")
  }

如果您需要在加载页面或进行其他任何事务后执行此操作,则可以使用ajax调用:

在菜单控制器中:

        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>

告诉我是否可行。

答案 1 :(得分:0)

您看到的不是@Html.Action("MenuLayout", "Menu"),而是

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

对于我之前遇到的类似情况,它已经为我工作了。