【问题标题】:MVC form post doesn't return render partial resultMVC 表单帖子不返回渲染部分结果
【发布时间】:2014-03-01 21:44:45
【问题描述】:

根据这里的一些帖子,我使用 Html.BeginFrom 而不是 Ajax.BeginForm,但在提交时我取得了控制权并返回带有部分视图的结果:

$('#SearchForm').on('submit', function (evt) {
    evt.preventDefault();
    $.post('/Search/Index', $(this).serialize(), function (response) {
        $('#searchResults').html(response); // assuming response is HTML
    });
});

这是我调用的操作:

    [HttpPost]
    public ActionResult Index(SearchModel searchModel)
    {
        if (ModelState.IsValid)
        {
            searchModel = CustomBehaviour.GetResults(searchModel);
            return PartialView("SearchResults", searchModel);
        }

        return View(searchModel);
    }

调试时调用了action,调用了js,也没有结果,请问是什么问题?

这里是视图:

        @using (Html.BeginForm("Index", "Search", FormMethod.Post, new { role = "form", @class = "form-horizontal", id="SearchForm" }))
        {
            @Html.AntiForgeryToken();
             @Html.EditorFor(m => m.Requirement)

             @Html.EditorFor(m => m.Location)


                    <button type="submit" class="btn btn-default">@Base.Search</button>

        }               

<div id="#searchResults">
</div>

【问题讨论】:

  • 是ModelState.IsValid 吗?

标签: jquery asp.net ajax asp.net-mvc asp.net-mvc-5


【解决方案1】:

问题是你的 div id:

<div id="#searchResults">
</div>

必须是:

<div id="searchResults">
</div>

【讨论】:

  • 天哪,我怎么会错过这个,我已经为此苦苦挣扎了很长时间!
  • 老实说,我设置了一个测试项目,有点看不出哪里出了问题。我们已经习惯了看到 jquery #id 选择器。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多