我有一個搜索功能的主頁。從主頁搜索工作正常,但是當我嘗試使用ajax表單(在url.com/search?id=biology上)再次搜索時,我被重定向到404頁面,說沒有找到/Search
。阿賈克斯形式沒有得到結果
首頁頁面:
@using (Html.BeginForm("Index", "Search", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Get, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(m => m.id, new { @class = "form-control" })
<input type="submit" value="Search" class="btn btn-default" />
}
我阿賈克斯形式:
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "searchResult" }))
{
<input class="form-control" data-val="true" data-val-required="The id field is required." id="id" name="id" type="search" value="@Request.QueryString["id"]" />
<span class="field-validation-valid text-danger" data-valmsg-for="id" data-valmsg-replace="true"></span>
<div class="form-group">
<input type="submit" value="Search" class="btn btn-default" />
</div>
}
@Html.Partial("_Results", Model)
編輯:這是所產生的Ajax表單的HTML源代碼。
<form action="/Search" data-ajax="true" data-ajax-method="get" data-ajax-mode="replace" data-ajax-update="#searchResult" id="form0" method="post">
<input class="form-control" data-val="true" data-val-required="The id field is required." id="id" name="id" type="search" value="biology" />
<span class="field-validation-valid text-danger" data-valmsg-for="id" data-valmsg-replace="true"></span>
<div class="col-md-offset-0 col-md-10">
<input type="submit" value="Search" class="btn btn-default" />
</div>
</form>
編輯2:從查看開發人員工具,它顯示POST調用正在發生。如何將它更改爲GET這個Ajax表單?
我想你忘記了喲添加動作和控制器的名稱'@ Ajax.BeginForm(「索引」,「搜索」,新AjaxOptions { ...})? – adricadar