我有一个简单的搜索页面,点击搜索按钮,应显示结果。
查看:
@model MvcSearchEngine.Models.SearchResultsModel
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<div>
<input id="txtSearch" type="text" />
<input id="search" type="submit" value="Search" />
</div>
<div id="data-grid">
</div>
<script type="text/javascript">
$(document).ready( function() {
$("#search").click(function () {
debugger;
$.ajax({
uri:"@Url.Action("PostSearchData","Search")",
cache: false,
type: 'POST',
async: true,
contentType: 'application/json; charset=utf-8',
data: '{ "filter": "' + $("#txtSearch").val() + '" }',
success: function(data)
{
debugger;
}
});
});
});
</script>
控制器:
public class SearchController : Controller
{
//
// GET: /Search/
SearchServiceClient searchProxy = new SearchServiceClient();
AdvertisementServiceClient AdvProxy = new AdvertisementServiceClient();
SearchResultsModel searchModel = new SearchResultsModel();
List<SearchServiceData> searchResult = new List<SearchServiceData>();
List<AdvertisementServiceData> advertisementResult = new List<AdvertisementServiceData>();
public ActionResult Index()
{
return View();
}
[AcceptVerbs("POST")]
public SearchResultsModel PostSearchData(SearchParam filter)
{
try
{
if (filter != null)
{
searchResult = searchProxy.GetSearchResultCriteria(Convert.ToString(filter));
advertisementResult = AdvProxy.GetAdvertisements(Convert.ToString(filter));
searchModel.SearchData = searchResult;
searchModel.AdvertisementData = advertisementResult;
}
return searchModel;
}
catch (Exception)
{
throw;
}
}
}
PostSearchData
控制器方法没有从搜索按钮上的ajax方法调用,只返回视图。我在做某个错误吗?
答案 0 :(得分:0)
确保您的操作标有以下属性,以便它可以响应POST调用:[HttpPost]
示例:
[HttpPost]
public ActionResult PostSearchData(string filter)
{
..
..
}