简体   繁体   English

ASP.net MVC 5从Home控制器调用布局中的局部视图

[英]ASP.net MVC 5 calling partial view thats in layout from Home controller

Hey all I am trying to find a way (or if its possible) to call this code: 嘿,我正在尝试寻找一种方法(或者如果可能的话)来调用此代码:

<nav>
    <ul class="nav navbar-nav">
        @if (Session["chosenAMT"] is bool == true)
        {
           @Html.Partial("_Menu")
        }
    </ul>
</nav>

When it starts the page the value for chosenAMT is false so the navigation does not load up on the _Layout.cshtml page. 当它启动页面时, selectedAMT的值为false,因此导航不会加载到_Layout.cshtml页面上。

However, once the user selects a value from the dropdown I need to enable that partial view to be called so that it can show to the user the navigation now. 但是,一旦用户从下拉菜单中选择一个值,我就需要启用该局部视图,以便它可以向用户显示导航。

When the user selects a value from the dropdown list I call a POST to the HomeController.cs : 当用户从下拉列表中选择一个值时,我将POST调用到HomeController.cs

$.ajax({
  type: 'POST',
  url: 'Home/showNav',
  cache: false,
  dataType: 'json',
  data: {},
  success: function (response) {
     console.log('worked');
  },
  error: function (response) {
     console.log(response);
  }
});

And this is the showNav code in the HomeController.cs : 这是HomeController.cs中showNav代码:

[HttpPost]
public string showNav()
{
   Dictionary<string, string> resultsBack = new Dictionary<string, string>();

   Session["chosenAMT"] = true;

   resultsBack.Add("dback", "GOOD");
   resultsBack.Add("dhead", "worked");
   resultsBack.Add("dinfo", "called nav");

   return JsonConvert.SerializeObject(allData, Formatting.Indented);
}

I'm pretty much stuck there as to how to go about making the call back to the _Layout.cshtml page in order to show the navigation now... 关于如何继续调用_Layout.cshtml页面以立即显示导航, 我非常 困惑

Any help would be great! 任何帮助将是巨大的! Thanks! 谢谢!

UPDATE 更新

Got it! 得到它了!

_Layout.cshtml: _Layout.cshtml:

 <nav id="menuContainer" data-url="@Url.Action("Menu","Home")">                    
    @Html.Action("Menu")
 </nav>

_Menu.cshtml: _Menu.cshtml:

 @if (Session["chosenAMT"].ToString() == "True")
 {
    <ul class="nav navbar-nav">
    etc...etc....
    </ul>
 }

index.js: index.js:

 success: function (response) {
    var url = $("#menuContainer").data("url");

    $("#menuContainer").load(url);
 },

HomeController: 家庭控制器:

 public ActionResult Menu()
 {
    return PartialView("_Menu");
 }

You need to dynamically reload that part of the menu. 您需要动态重新加载菜单的该部分。 To start, create an action method which returns the markup for the menu. 首先,创建一个操作方法,该方法返回菜单的标记。

public ActionResult Menu()
{
   return PartialView("Menu");
}

You may also move your if condition statement inside the partial view to conditionally render some markup. 您也可以在局部视图内移动if条件语句,以有条件地呈现一些标记。 So your Menu.cshtml will be like 所以你的Menu.cshtml就像

    <ul class="nav navbar-nav">
        @if (Session["chosenAMT"] is bool == true)
        {
           @Html.Partial("_Menu")
        }
    </ul>

Now call this action method inside the outer container element. 现在,在外部容器元素内部调用此action方法。

<nav id="menuContainer" data-url="@Url.Action("Menu","YourControllerName")">
   @Html.Action("Menu")
</nav>

Now all you have to do is, reloading the content of this menu container div on the success event of your ajax call. 现在,您要做的就是在ajax调用成功时重新加载此菜单容器div的内容。 You can use the jQuery load method 您可以使用jQuery load方法

success: function (response) {
     var url = $("#menuContainer").data("url");
     $("#menuContainer").load(url);
},

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

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