2015-02-06 42 views
0

我搜索了高和低,無法找到答案,大量的ajax和無關鏈接。只有在C#MVC的請求(沒有aspx文件等)谷歌CSE在MVC4,使用表格時遇到問題

我想使用一個cshtml中的表單閱讀查詢到谷歌cse,所以我使用谷歌結果只有頁選項CSE。問題在於他們沒有指定如何將結果輸出到JavaScript,並且只在10年前簡單地完成了js,我無法弄清楚如何執行此操作並需要一些幫助。

我想將我的查詢發送到搜索控制器,然後生成帶有谷歌搜索結果的頁面。 google javascript在佈局頁面中列出。在它的谷歌搜索

頭CSHTML文件

<form class="navbar-form input-group-sm btn-group" gname="gsearch" role="search" method="post" action="@Url.Action("Index", "Search")"> 
      <div class="navbar-form input-group-addon input-group-sm btn-group-sm" style="background-color:transparent"> 
       <input class="form-control" name="q" size="20" style="vertical-align:bottom;margin:0px;padding:0px" maxlength="255" value="search site" onclick="value = '';" /> 
       <button class="btn btn-default btn-sm" type="submit" style="margin:0px"><i class="glyphicon glyphicon-search" style="margin:0px;"></i></button> 
      </div> 
     </form> 

控制器

public class SearchController : Controller 
{ 

    // 
    // GET: /Search/ 
    [HttpPost] 
    public ActionResult Index(string q) 
    { 
     return View(); 
    } 
} 

搜索頁面.cshtml

<div class="gcse-searchresults" > 
<gcse:searchresults-only linkTarget="_top" gname="gsearch"></gcse:searchresults-only> 

想通了事情的原委: 在我的聲明我應該使用方法=「獲取」,而不是方法後,我應該申報[HTTPGET]在SearchController而非[HttpPost],代碼工作的功能之前,現在好了。

+0

剛剛發現我應該使用HttpGet而不是HttpPost調用 – Slipoch 2015-02-06 05:22:20

回答

0

在控制器需要[HTTPGET]代替程序[HttpPost],無需可變(作爲JS處理它)

[HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

在表單的第一行所需要從改變header.cshtml方法= 「郵報」 的方法= 「獲得」

<form class="navbar-form input-group-sm btn-group" gname="gsearch" role="search" method="get" action="@Url.Action("Index", "Search")"> 

希望這有助於其他人使用MVC & CSE整合。