如何访问当前控制器名称在我的Razor视图jquery

how to access the current controller name inside my Razor view jquery

本文关键字:我的 Razor jquery 视图 控制器 何访问 访问      更新时间:2023-09-26

我有以下脚本,这是在多个视图中使用:-

 $("#ChoiceTag, #ChoiceName").each(function () {
            $(this).change(function () {
                if ($("#ChoiceName").prop("checked")) {
                    $.getJSON("@Url.Content("~/Firewall/LoadCSName")",
                    function (CSData) {
                        var select = $("#GeneralCSID");
                        select.empty();
                        select.append($('<option/>', {
                            value: "",
                            text: "Select Name..."
                        }));
                        $.each(CSData, function (index, itemData) {
                            select.append($('<option/>', {
                                value: itemData.Value,
                                text: itemData.Text
                            }));
                            select.val('@Model.CSIT360ID');
                    });
                });
            }

脚本对于所有视图都是完全相同的,除了下面语句中的控制器名:-

$.getJSON("@Url.Content("~/Firewall/LoadCSName")",

所以我希望移动上面的脚本并将其添加到一个单独的。js文件中,然后引用这个脚本,但我有以下两个问题:-

  1. 如果我将脚本移动到脚本文件夹,我需要动态引用当前控制器名称来构建URL,所以这是可能的

  2. 我仍然可以引用viewbag,因为我目前正在做…由于

如果你把Javascript移到外部文件中,你就不能使用Razor语法了。因此,@Url.Content("~/Firewall/LoadCSName")不能解析

要克服这个问题,请将此添加到视图

<script type="text/javascript"> var AppPath = '@Url.Content("~/")'</script>

并在脚本中引用它,像这样

$.getJSON(AppPath + "Controller/Action")

关于viewbag。只要把viewbags的值放在如上所示的变量中,你的外部文件就可以引用它。

希望有帮助

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>

    <script type="text/javascript">
        var AppPath = '@Url.Content("~/")';
        var SomeValue = '@Model.CSIT360ID';
        var ControllerName = "Firewall/LoadCSName";
    </script>
    <!--Move this to an external File-->
    <script type="text/javascript">
        $("#ChoiceTag, #ChoiceName").each(function () {
            $(this).change(function() {
                if ($("#ChoiceName").prop("checked")) {
                    $.getJSON(AppPath + ViewBagValue), function(CSData) {
                            var select = $("#GeneralCSID");
                            select.empty();
                            select.append($('<option/>', {
                                value: "",
                                text: "Select Name..."
                            }));
                            $.each(CSData, function(index, itemData) {
                                select.append($('<option/>', {
                                    value: itemData.Value,
                                    text: itemData.Text
                                }));
                                select.val(SomeValue);
                            });
                            //end each
                    });
                }
            });
    </script>
</body>
</html>

更新2

这是你在url.content

中引用控制器的方式
<script type="text/javascript">
    var AppPath = '@Url.Content("~/" + HttpContext.Current.Request.RequestContext.RouteData.Values["controller"])'
</script>

您可以通过以下方式获取控制器名称:

   @{
string controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
}

从视图中使用

访问控制器名称
@{
    ViewContext.RouteData.Values["controller"].ToString();
}

要访问控制器实例,可以使用如下命令

@{
    (HomeController)ViewContext.Controller
}

让JS可以访问当前控制器名的一种方法是将其写入布局中的全局或命名空间变量赋值中。

<script>
var app = window.app || {}
app.currentController = "@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString().toLower()";
</script>

或者,我工作的一种常用方法是将当前控制器和动作的类名添加到body标记中,以帮助在任何javascript中基于DOM的路由。