asp.net mvc controller controll partialview show / not show

时间:2015-12-02 21:18:53

标签: asp.net-mvc asp.net-mvc-partialview

假设有一个_Layout.cshtml,并且在底部(即使在脚本src' s下),还有一个Html.Partial调用一个名为" _Remodal"的部分视图。

底部_Layout.cshtml

<script src="@Url.Content("~/Content/scripts/show-validation-error.js")"></script>
<script src="@Url.Content("~/Content/scripts/app.js")"></script>

@Html.Partial("_Remodal")

_Remodal

<script>
  $(document).ready(function() {
      window.showOeps('oeps');
  })
</script>

而且,这个局部视图会打开这样的东西: enter image description here

我有什么方法可以在控制器内说:&#39;在_Layout.cshtml上显示这个部分&#39;?

控制器,例如

[HttpPost]
public ActionResult SignIn(string email, string password)
{
    try
    {
        var user = userRepository.AuthorizedUser(email, password);
        if (user != null)
        {
            return View("~/Views/Questions/_Questions.cshtml");
        }
        else
        {
            // show @Html.Partial("_Remodal") on _Layout.cshtml
        }
    }
    catch (Exception)
    {
        throw;
    }
}

我已经尝试过使用ajax并返回javascript,但它对我没用。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用ViewBag设置一个标志,告诉您是否要显示它。在局部视图中检查并隐藏/显示对话框。

[HttpPost]
public ActionResult SignIn(string email, string password)
{
     try
     {
        var user = userRepository.AuthorizedUser(email, password);
        if (user != null)
        {
             return View("~/Views/Questions/_Questions.cshtml");
        }
        else
        {
             ViewBag.ShouldShowDialog = "Show";                
             return View("NotAuthorizedView");
         }
     }
     catch (Exception ex)
     {
            //log ex and show an error view to user
     }
  }

在偏见视图中,

<script>
  $(document).ready(function() {
     var shouldShow = "@ViewBag.ShouldShowDialog";
     if(shouldShow === "Show")
     {
        window.showOeps('oeps');
     }
  })
</script>