2014-11-03 31 views
0

我的Ajax.BeginForm沒有將搜索條件提交給控制器。代碼如下:Ajax.BeginForm搜索不在Controller中調用方法(MVC 4)

@using (Ajax.BeginForm("Index_AddSearchCriteria", "Home", new AjaxOptions {HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "SearchDiv" })) 
{ 

     // @Html.Label("Search: ") // should I use this instead? 
     // @Html.TextBox("search_criteria") // should I use this instead? 
     <input type="text" name="search_criteria" /> 
     <input type="submit" value="Search" /> 
} 

<div id="SearchDiv"> 

</div> 

,並在控制器

public PartialViewResult Index_AddSearchCriteria(string search_criteria) 
    { 
     ViewModel.SearchCriteria = search_criteria; 

     return PartialView("SearchBar", ViewModel); 

    } 

我已經把我的Index_AddSearchCriteria一個斷點,當我按下搜索按鈕永遠不會連撞。

我有jquery和unobtrusive-ajax加載。

編輯: 這是錯誤,我得到 -

POST http://localhost:51989/Home/Index_AddSearchCriteria 404 (Not Found) jquery-1.8.2.js:8430 
send jquery-1.8.2.js:8430 
jQuery.extend.ajax jquery-1.8.2.js:7982 
asyncRequest jquery.unobtrusive-ajax.js:121 
(anonymous function) 
jquery.unobtrusive-ajax.js:171 
jQuery.event.dispatch jquery-1.8.2.js:3077 

elemData.handle.eventHandle

編輯2: 因此,代碼的工作,但我也有同樣的另一PartialView頁面使用Ajax並每100毫秒輪詢一次,這似乎使發送搜索查詢花費很長時間。我已將其更改爲1000毫秒,搜索正常。

+0

使用瀏覽器的開發者工具或使用像fiddler這樣的程序來查看ajax請求是否實際發送。如果是,你應該看到迴應和任何錯誤回來。 – heymega 2014-11-03 14:57:44

+0

它正在發送,但我得到一個404錯誤,沒有找到Index_AddSearchCriteria – prunes4u 2014-11-03 15:24:54

+0

我剛剛運行您的代碼,它運行正常。 (我假設代碼正在爲你構建,因爲ViewModel沒有在你給的示例中定義,所以我假定它定義並創建在其他地方)。你玩過你的路線嗎? – KevDevMan 2014-11-03 15:28:08

回答

1

我能夠複製您的問題。基本上,你不是在做一個AJAX的「Get」(因此POST中的錯誤),你只是在做一個標準的Web帖子。我最終能夠解決這個問題。看來,jquery.unobtrusive-ajax有一個問題,你需要改變代碼,使所有的「.live」事件變成「.on」事件。

$("a[data-ajax=true]").live("click", function (evt) { 
    evt.preventDefault(); 
    asyncRequest(this, { 
     url: this.href, 
     type: "GET", 
     data: [] 
    }); 
}); 

爲 - (請不有幾個.live事件,所以你應該改變所有這些也不要引用分鐘文件,除非你改變也)

$("a[data-ajax=true]").on("click", function (evt) { 
    evt.preventDefault(); 
    asyncRequest(this, { 
     url: this.href, 
     type: "GET", 
     data: [] 
    }); 
}); 
相關問題