簡體   English   中英

無法在 Asp.net 中通過控制器調用新頁面

[英]Can't call new page by controller in Asp.net

我想通過單擊Index.cshtmljQuery按鈕來調用新頁面:

$("#btnSearch").click(function(){
        var idser = $("#Name").val();

        $.ajax({    
           type: "POST",
           url: "/User/getAll",
           id : idser,
           success: function() {
               alert("success");
           }
       });
    }); 
});

它調用我的控制器操作: UserController/getAll

[System.Web.Services.WebMethod]
public ActionResult getAll(string id,string name)
{
    return View("AllUser"); 
}

但是還是在Index.cshtml不去AllUser.cshtml頁面? 我不知道為什么...請幫忙。 更新:我的 jquery 函數在控制器中調用我的操作,並且操作正常工作但它不返回到 AllUser.cshtml 頁面。

更新以回應評論中的澄清:

success回調中,

location.href="/user/getall";

這將導致瀏覽器在 ajax 發布完成后導航到該 URL。


將輸入中的值發布到另一個頁面的按鈕 - 這是一個表單。

<form action="/User/getAll">
    <input type="submit" value="Click me">
</form>

我不知道你的 id 來自哪里,但你可以在表單中放置一個隱藏的輸入和一個腳本來填充它(除非它是用戶輸入 - 然后你可以將輸入放在表單中。)

<form action="/User/getAll">
    <input type="submit" value="Click me">
    <input type="hidden" name="id" id="hiddenId"/>
</form>

<script>
    $("#hiddenId").val($("#Name").val());
</script>

或者,如果您想確保表單的action URL 與您的路由匹配:

<form action='@Url.Action("getAll", "User")'>

(假設控制器被稱為“用戶”。)

請在此處查看文檔。 我會嘗試在Ajax設置中使用標簽助手,如下所示:

$("#btnSearch").click(function(){
        var idser = $("#Name").val();

        $.ajax({    
           type: "POST",
           url: '@Url.Action("User", "getAll", new { id = "ID", name = "searchName" })',
           id : idser,
           success: function() {
               alert("success");
           }
       });
    }); 
});

首先,我不知道您在重定向其他頁面時使用 ajax 的目的是什么? 但是如果你仍然想使用 Ajax,你可以通過兩種方式來實現:1) 你可以在你的成功塊中分配window.location

2)通過以下方式使用您的代碼:

$("#btnSearch").click(function(){
        var idser = $("#Name").val();

        $.ajax({    
           type: "GET",
           url: "/User/getAll",
           id : idser,
           success: function() {
               alert("success");
               return true;
           }
       });
    }); 
});

您可以通過在腳本中放置return true來嘗試它。

希望對你有幫助

開始了:

window.location.href = "yourUrl" 可以幫助你。

 $("#btnSearch").click(function(){
            var idser = $("#Name").val();

            $.ajax({    
               type: "POST",
               url: "/User/getAll",
               id : idser,
               success: function() {
                   alert("success");
                  window.location.href ="../yourUrl";  //This is working!
               }
           });
        }); 
    });

您可以像這樣在 Controller 中重定向:

[System.Web.Services.WebMethod]
public ActionResult getAll(string id,string name)
{
    return RedirectToAction("NameOfAction"); //Here is going Name of Action       wich returns View AllUser.cshtml

}

希望能幫助到你;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM