Asp.net mvc MVC Ajax格式赢得';不要屈服

Asp.net mvc MVC Ajax格式赢得';不要屈服,asp.net-mvc,asp.net-mvc-4,asp.net-ajax,Asp.net Mvc,Asp.net Mvc 4,Asp.net Ajax,在我的MVC4应用程序中,我在master/layout视图中有一个用于搜索的Ajax表单,以及使用相同布局视图的Add表单视图。在这个包含两个表单的页面上,Ajax表单将不起作用,或者不会在Add将提交的地方提交 Ajax表单是使用局部视图创建的,它可以在没有其他表单的所有页面上工作。有什么办法来完成这项工作吗?谢谢 控制器动作 public ActionResult SearchResults(SearchFormViewModel filterModel, string searchTe

在我的MVC4应用程序中,我在master/layout视图中有一个用于搜索的Ajax表单,以及使用相同布局视图的Add表单视图。在这个包含两个表单的页面上,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 />

}
@model SFRS.DataMarketPlace.Web.Models.SearchFormViewModel
@使用(Ajax.BeginForm(“SearchResults”,null,新的AjaxOptions{UpdateTargetId=“results”},新的{@id=“filterm”,@role=“form”}))
{
@Html.ValidationSummary(true,“表单提交未成功。请更正错误并重试。”)
@Html.AntiForgeryToken()
搜索词
@TextBoxFor(m=>m.SearchTerm,新的{@class=“form control”,maxlength=“64”,placeholder=“搜索数据集…”)
@Html.ValidationMessageFor(m=>m.SearchTerm)

}
添加视图

@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>

    }
@使用SFRS.DataMarketPlace.Web.Models
@使用Microsoft.Web.Helpers
@模型SFRS.DataMarketPlace.Web.Models.DatasetViewModel
@{
ViewBag.Title=“添加”;
Layout=“\u Layout\u noBanner.cshtml”;
}
添加新数据集

请尽可能多地填写您所知道的内容,我们将填写其余内容。标有红色星号*的字段是必填字段。

@使用(@Html.BeginForm(“Save”,“Dataset”,FormMethod.Post,new{@class=“form horizontal”,id=“DocumentForm”,enctype=“multipart/form data”})) { //一些领域
@**@保存 }
试试这个:

查看:

@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)
控制器:

@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)
还可以尝试以下方法:

查看:

@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)
控制器:

@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)

如果这不起作用,请告诉我。

控制台中有错误吗?第二个问题。您的项目中是否有
jQuery不引人注目的Ajax
?如果我没有错,这不是默认的控制台中没有错误,是的,我确实在页面上有Jquery Unobtrusive Ajax控制器操作是什么样子的?我已经添加了控制器操作代码。问题可能在这里:
public ActionResult SearchResults(SearchFormViewModel filterModel,string searchTerm=“”)
您在“searchTerm”表单中只有一个字段,我相信这是发送给控制器的唯一内容。但控制器方法正在等待2个参数。请评论SearchFormViewModel filterModel,并检查它现在是否进入该方法