簡體   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