Ajax表单没有得到结果
本文关键字:结果 表单 Ajax | 更新日期: 2023-09-27 17:51:10
我有一个带有搜索功能的主页。从主页搜索工作正常,但当我尝试使用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" />
}
My Ajax Form:
@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调用。我如何改变它为这个ajax表单GET ?
@User456789如果您将查看这两种情况下生成的表单,那么您将注意到Ajax表单的action
将不同于您的主页表单。这是因为您没有像上面评论中提到的那样在Ajax帮助器中提到Controller
和Action
。因此,像下面这样更改Ajax表单代码,它应该按预期工作:
@using (Ajax.BeginForm("Index", "Search", 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>
}
变化:
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "searchResult" }))
:
@using (Ajax.BeginForm("Index", "Search", new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "searchResult" }))
您需要将表单指向MVC控制器中的正确操作。确保Index
存在并返回Content
。