簡體   English   中英

從JavaScript調用視圖

[英]Calling view from JavaScript

我有一個像這樣定義的JavaScript函數:

function onQuickSearchClick(s) {
    var query = s.GetText();

    $.post('/Search/', { query: query });
}

現在我想用SearchController中的查詢文本調用View“Search”。 我怎樣才能做到這一點?

執行此操作時,不顯示任何視圖:

SearchController.cs:

    public ActionResult Index(string query)
    {
        // Can't do "return View(query)" here, because this would be interpreted as the view name
        return View();
    }

如何將查詢參數傳遞給我的Views / Search / Index.cshtml?

function onQuickSearchClick(s) {
    var query = s.GetText();

    window.location = '@Url.Action("Index", "Search")?query=' + query; 

    /* OR
    window.location = 'Search/Index?query=' + query; 
    */

    //$.post('/Search/', { query: query });
}

我想我沒理解。 你可以像這樣返回字符串作為模型:

public ActionResult Index(string query)
{
    return View((object)query);
}

然后你的MVC將知道query是一個模型而不是viewName

你必須使用三個參數重載。

第一個參數是視圖名稱,第二個參數是主視圖名稱,最后一個是模型。

return View("Index", "", query);

暫無
暫無

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

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