在我的MVC 4应用程序中,我在master / layout视图中有一个用于搜索的Ajax表单,并且使用相同的布局视图有一个Add form视图。在这个包含两个表单的页面上,Ajax表单将不起作用或不会提交Add将提交的位置。
使用局部视图创建Ajax表单,它可以在我们没有其他表单的所有页面上运行。有关如何使这项工作的任何想法?感谢
控制器操作
public ActionResult SearchResults(SearchFormViewModel filterModel, string searchTerm = "")
{
if (string.IsNullOrEmpty(filterModel.SearchTerm) && !string.IsNullOrEmpty(searchTerm))
{
filterModel.SearchTerm = searchTerm;
}
var filter = DataMarketFilter.NewInstance();
filter.SearchTerm = filterModel.SearchTerm;
filter.Theme = filterModel.Themes;
filter.Publisher = filterModel.Publishers;
filter.Coverage = filterModel.Coverage;
filter.Year = filterModel.Year;
filter.SortBy = filterModel.SortBy;
SearchResultsModel model = this.UnitOfWork.DatasetRepository.SearchProfiles(filter);
return this.View(model);
}
搜索表单部分视图
@model SFRS.DataMarketPlace.Web.Models.SearchFormViewModel
@using (Ajax.BeginForm("SearchResults", null, new AjaxOptions { UpdateTargetId = "results" }, new { @id = "FilterForm", @role = "form" }))
{
@Html.ValidationSummary(true, "Form submission was unsuccessful. Please correct the errors and try again.")
@Html.AntiForgeryToken()
<div class="input-group stylish-input-group col-md-12 pull-left">
<label class="sr-only" for="Search Term">Search Term</label>
@Html.TextBoxFor(m => m.SearchTerm, new { @class = "form-control", maxlength = "64", placeholder = "Search For Datasets ..." })
@Html.ValidationMessageFor(m => m.SearchTerm)
<span class="input-group-addon">
<button type="submit" name="Send" value="Search">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<br />
}
添加视图
@using SFRS.DataMarketPlace.Web.Models
@using Microsoft.Web.Helpers
@model SFRS.DataMarketPlace.Web.Models.DatasetViewModel
@{
ViewBag.Title = "Add";
Layout = "_Layout_noBanner.cshtml";
}
<div class="container">
<div class="well">
<h4 class="strong">Add New Dataset</h4>
<p>
Please complete as much as you know, and we will fill in the rest. Fields marked with red asterisk <font color="red">*</font>are mandatory.
</p>
</div>
<div class="col-lg-8 well">
@using (@Html.BeginForm("Save", "Dataset", FormMethod.Post, new { @class = "form-horizontal", id = "DocumentForm", enctype = "multipart/form-data" }))
{
// some fields
<br />
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" value="Submit" class="btn btn-primary">
@* <span class="glyphicon glyphicon-floppy-disk"></span>*@Save
</button>
</div>
</div>
}
答案 0 :(得分:0)
试试这个:
查看:强>
@using (Ajax.BeginForm("Test", "YOURCONTROLLERNAME", new { searchTerm = "" }, null))
<强>控制器:强>
[HttpPost]
public ActionResult SearchResults(SearchFormViewModel filterModel, string searchTerm)
也试试这个:
查看:强>
@using (Html.BeginForm("YOURCONTROLLERNAME", "SearchResults"))
<强>控制器:强>
[HttpPost]
public ActionResult SearchResults(SearchFormViewModel filterModel)
如果这不起作用,请告诉我。