MVC 从不同的视图加载带有授权标签的控制器动作
MVC Loading Controller Action with Authorize Tag from Different View
我创建了一个 bootstrap 模态,它调用控制器操作以在模态中加载视图。
控制器操作有一个授权标签,这意味着每当你加载默认视图(不是模态)它都会要求你登录,模态包含创建新功能,我不希望匿名用户访问因此是授权标签。
但用户无法访问任何内容,因为视图正在调用创建控制器操作(具有授权标记),即使模式尚未打开也是如此。
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog" style="width: 70%;">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title text-center">Create New Connecting Rod</h3>
</div>
<div class="modal-body" id="createModal">
@Html.Action("Create")
</div>
</div>
</div>
</div>
以上代码是用于调用创建控制器操作的代码,我只希望在单击模态按钮时调用控制器操作,而不是在首次加载页面时调用。
[Authorize]
public ActionResult Create()
{
return PartialView();
}
这是我的创建控制器操作中唯一的代码
我曾尝试使用某些 JQuery 仅在按下按钮后才加载动作,但似乎也没有用。
$(document).on("click", "#createNew", function (e) {
alert("hello");
$('@Html.Action("Create")').appendTo('#createModal');
});
不要在页面加载时加载您的授权内容,因为用户可能已登录或匿名。而是在用户单击 link 或按钮时加载内容,然后调用打开模式对话框方法。
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog" style="width: 70%;">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title text-center">Create New Connecting Rod</h3>
</div>
<div class="modal-body" id="createModal">
<!-- have some loading icon here by default -->
</div>
</div>
</div>
</div>
前提是你有一个按钮可以打开对话框写下面的脚本
<script>
$(function{
$("your_button").click(function(){
// load content of your url @Url.Action("Create") in div "#createModal"
// and then open the dialog
$.ajax({
url: "@Url.Action("Create")",
cache: false,
dataType: "html",
success: function (data) {
$("#createModal").html(data);
}
});
$("#myModal").modal().show();
});
});
</script>
我创建了一个 bootstrap 模态,它调用控制器操作以在模态中加载视图。
控制器操作有一个授权标签,这意味着每当你加载默认视图(不是模态)它都会要求你登录,模态包含创建新功能,我不希望匿名用户访问因此是授权标签。
但用户无法访问任何内容,因为视图正在调用创建控制器操作(具有授权标记),即使模式尚未打开也是如此。
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog" style="width: 70%;">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title text-center">Create New Connecting Rod</h3>
</div>
<div class="modal-body" id="createModal">
@Html.Action("Create")
</div>
</div>
</div>
</div>
以上代码是用于调用创建控制器操作的代码,我只希望在单击模态按钮时调用控制器操作,而不是在首次加载页面时调用。
[Authorize]
public ActionResult Create()
{
return PartialView();
}
这是我的创建控制器操作中唯一的代码
我曾尝试使用某些 JQuery 仅在按下按钮后才加载动作,但似乎也没有用。
$(document).on("click", "#createNew", function (e) {
alert("hello");
$('@Html.Action("Create")').appendTo('#createModal');
});
不要在页面加载时加载您的授权内容,因为用户可能已登录或匿名。而是在用户单击 link 或按钮时加载内容,然后调用打开模式对话框方法。
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog" style="width: 70%;">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title text-center">Create New Connecting Rod</h3>
</div>
<div class="modal-body" id="createModal">
<!-- have some loading icon here by default -->
</div>
</div>
</div>
</div>
前提是你有一个按钮可以打开对话框写下面的脚本
<script>
$(function{
$("your_button").click(function(){
// load content of your url @Url.Action("Create") in div "#createModal"
// and then open the dialog
$.ajax({
url: "@Url.Action("Create")",
cache: false,
dataType: "html",
success: function (data) {
$("#createModal").html(data);
}
});
$("#myModal").modal().show();
});
});
</script>