繁体   English   中英

获取在ASP.NET MVC中调用控制器的视图的名称

[英]Get the name of view who called controller in ASP.NET MVC

有什么方法可以获取在控制器中调用方法的View的名称并将其保存在该控制器的方法内部的某个自定义变量中?

例如:

我有一个使用Ajax来获取控制器中InfinateScroll方法的视图:

<div class="container-post">
    <div id="postListDiv">
        @{Html.RenderAction("PostList", "Posts", new { Model = Model });}
    </div>
    <div id="loadingDiv" style="text-align: center; display: none; margin-bottom: 20px;">
        <img alt="Loading" src="@Url.Content("~/images/ajax-loader.gif")" />
    </div>
</div>

    <script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
    <script type="text/javascript">
        var BlockNumber = 2;
        var NoMoreData = false;
        var inProgress = false;

        $(window).scroll(function () {
            if ($(window).scrollTop() == $(document).height() - $(window).height() && !NoMoreData && !inProgress) {
                inProgress = true;
                $("#loadingDiv").show();

                $.post("@Url.Action("InfinateScroll", "Posts")", { "BlockNumber": BlockNumber },
                    function (data) {
                        BlockNumber = BlockNumber + 1;
                        NoMoreData = data.NoMoreData;
                        $("#postListDiv").append(data.HTMLString);
                        $("#loadingDiv").hide();
                        inProgress = false;
                    });
            }
        });
    </script>

我在两页上使用此视图。 在一种情况下,我使用它仅显示特定用户(已登录用户)的帖子,而在另一种视图中,我显示数据库中所有用户的帖子(类似于Facebook墙,您只能看到您的帖子,以及NewsFeed,您不仅可以在其中发表您的评论,还可以在其中发表评论。

由于某些原因,我想知道在调用InfinateScroll方法时哪个页面处于活动状态。

我想在这两个页面之间进行一些区别,以便稍后再进行检查。

[HttpPost]
public ActionResult InfinateScroll(int BlockNumber)
{
     int BlockSize = 5;
     var posts = PostManager.GetPosts(BlockNumber, BlockSize);
     JsonModel jsonModel = new JsonModel();
     jsonModel.NoMoreData = posts.Count < BlockSize;
     jsonModel.HTMLString = RenderPartialViewToString("PostList", posts);

     return Json(jsonModel);
}

此方法使用辅助方法GetPosts获取帖子,用于滚动显示更多帖子。

您可以使用以下方法从视图内部获取当前视图的名称:

@Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))

来源: 如何在asp.net MVC 3中获取当前视图名称?

因此您可以将其作为路由值添加到@ Url.Action中,如下所示:

@Url.Action(
    "InfinateScroll",
    "Posts",
    new{callingView=Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))})

然后可以向控制器方法添加参数

public ActionResult InfinateScroll(int BlockNumber, string callingView)

您可以像这样在html中创建一个隐藏变量-

<input type="hidden" id="pageName" value="myPage1" />

为您的操作添加一个额外的参数-

public ActionResult InfiniteScroll(int BlockNumber, int pageName)

然后,在您的jquery代码中,当您发布时,也要发送pageName。

$.post("@Url.Action("InfinateScroll", "Posts")", { "BlockNumber": BlockNumber, "pageName": $('#pageName').val() },

希望这可以帮助。

在一种情况下,我正在使用它仅显示特定用户的帖子...而在另一种情况下,我正在显示数据库中所有用户的帖子...

在视图上放置所需的逻辑是不安全的,尤其是在显示数据是基于用户或特定于用户的情况下。 但是,如果您坚持要在视图上包含逻辑,则应该将另一个变量传递给控制器​​,如下所示:

$.post("@Url.Action("InfinateScroll", "Posts")", 
{ "BlockNumber": BlockNumber, "UserId": userId },
    // rest of your code goes here...
});

然后,您应该在控制器中具有另一个参数:

[HttpPost]
public ActionResult InfinateScroll(int BlockNumber, int userId)
{
    //filter your data based on the "userId" parameter
}

但是就像我提到的那样,这是不安全的,因为有人可以轻松地传递有效的“ userId”,并在您不想要的时候访问数据。 因此,最安全(或更安全)的方法是在控制器中使用“过滤逻辑”,如下所示:

[HttpPost]
public ActionResult InfinateScroll(int BlockNumber)
{
    // a context based logic
    var userId = GetLoggedInUserId();
    // that method could return null or zero 
    // and depending on how you approach it       
    //filter your data based on the "userId" 
}

暂无
暂无

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

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