asp.net mvc 控制器控制器部分视图 show/not 显示
asp.net mvc controller controll partialview show/not show
假设有一个 _Layout.cshtml
,在底部(甚至在脚本 src 的下面)有一个 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>
并且,此部分视图将打开如下内容:
我有什么办法可以在控制器中说:'Show this partial on the _Layout.cshtml'?
控制器例如
[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,但它对我不起作用。
谢谢!
您可以使用 ViewBag 设置一个标志,告诉您是否要显示它。检查您的局部视图和 hide/show 对话框。
[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>
假设有一个 _Layout.cshtml
,在底部(甚至在脚本 src 的下面)有一个 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>
并且,此部分视图将打开如下内容:
我有什么办法可以在控制器中说:'Show this partial on the _Layout.cshtml'?
控制器例如
[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,但它对我不起作用。
谢谢!
您可以使用 ViewBag 设置一个标志,告诉您是否要显示它。检查您的局部视图和 hide/show 对话框。
[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>