【问题标题】:Ajax.BeginForm() Action is not firedAjax.BeginForm() 动作未触发
【发布时间】:2014-06-08 21:35:13
【问题描述】:

这是我的 View.cshtml

<h3>Search One Drive</h3>

<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

@using (Ajax.BeginForm("test", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "result" }))    
{
    @Html.ValidationSummary(true)
<fieldset>
    <legend>Search</legend>

    <div id="result"></div>
    <div class="editor-label">
            @Html.LabelFor(model => model.queryTerm)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.queryTerm)
            @Html.ValidationMessageFor(model => model.queryTerm)
        </div>
</fieldset>
<p>
    <input type="submit" value="Submit" />
</p>
}

我的 Home Controller 中有相应的 ActionResult:

[HttpPost]
        public ActionResult test(Model testModel)
        {
            string query = testModel.queryTerm;
            return Content("hi " + query);
        }

这是一个带有剃须刀的 MVCV4 网络应用程序。因此,当我进入查看页面并输入查询并将鼠标悬停在“提交”上时,我会看到 Home/test 方法显示(悬停时)。但是当我点击提交时,控件并没有传递到 HomeController 中的 ActionResult test() 上。

我错过了什么吗?

谢谢!

【问题讨论】:

  • 你能显示完整的控制器吗
  • 我有点担心您选择使用Model 作为参数。您能否展示您的视图模型代码和完整的视图源代码,包括@model 声明?

标签: jquery ajax asp.net-mvc-4 razor ajaxform


【解决方案1】:

好的,这就是修复:

我的模型类中没有无参数构造函数。因此,在进行此调用时,它无法创建实例。有一次,我添加了默认构造函数,它开始运行良好。

在 Fiddler 的帮助下解决了这个问题。

【讨论】:

    猜你喜欢
    • 2014-12-09
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 2014-03-16
    相关资源
    最近更新 更多