簡體   English   中英

我進行ajax調用后,為什么控制器沒有顯示我的視圖?

[英]Why isn't my controller displaying my view after I make an ajax call?

我正在使用javascript中的Ajax調用將控制器操作稱為“結果”,但頁面沒有刷新或進入視圖“搜索”! 它停留在我當前所在的頁面上。

    [HttpGet]
    public ActionResult Results(string searchQuery) //, DateTime classDate)
    {
        var viewModel = new YogaSpaceListViewModel
        {
            SearchQuery = searchQuery
        };
        return View("Search", viewModel);
    }

  $.ajax({ method: "GET", url: "/home/results", data: { searchQuery: searchQuery } }). fail(function(jqXHR, textStatus) { alert("Request failed: " + textStatus); }); 

我認為您對AJAX調用是什么有點困惑,這些請求不會將用戶發送到另一個頁面,也不會將您發送到該頁面的編寫方式-實際上,除了進行調用之外,它什么都不會做。

 $.ajax({
   method: "GET",
   url: "/home/results",
   data: {
     searchQuery: searchQuery
   },
   success: function(data){
    // here is what is called on a successful response from the server
    // it is now your job to do something with the response.
    // the response will be found in the `data` object
   }
 }).

您要做的是將響應中的數據放入html中的某處,像注釋之類的東西就可以了。

  $('#myDiv').html(data);

我不建議您做所有這些事情,而是推薦這樣的事情,因為您似乎要這樣做:

   window.location  = "/Home/Results?searchQuery=" + searchQuery;

這會將用戶轉到您希望他們訪問的新頁面。

暫無
暫無

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

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