【问题标题】:MVC Ajax form won't submitMVC Ajax 表单不会提交
【发布时间】:2014-11-28 11:40:53
【问题描述】:

在我的 MVC 4 应用程序中,我有一个用于在主/布局视图中搜索的 Ajax 表单和一个使用相同布局视图的添加表单视图。在这个包含两个表单的页面上,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>

    }

【问题讨论】:

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

标签: asp.net-mvc asp.net-mvc-4 asp.net-ajax


【解决方案1】:

试试这个:

查看:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2020-10-21
    • 2012-09-23
    相关资源
    最近更新 更多