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毫秒,搜索正常。
使用瀏覽器的開發者工具或使用像fiddler這樣的程序來查看ajax請求是否實際發送。如果是,你應該看到迴應和任何錯誤回來。 – heymega 2014-11-03 14:57:44
它正在發送,但我得到一個404錯誤,沒有找到Index_AddSearchCriteria – prunes4u 2014-11-03 15:24:54
我剛剛運行您的代碼,它運行正常。 (我假設代碼正在爲你構建,因爲ViewModel沒有在你給的示例中定義,所以我假定它定義並創建在其他地方)。你玩過你的路線嗎? – KevDevMan 2014-11-03 15:28:08