我对MVC 5 Ajax.Begin表单感到困惑。我过去多次使用它没有任何问题。
常见问题是开发人员忘记包含jquery.unobtrusive-ajax.js文件,但在这种情况下不会。
我在appSetting部分的webconfig中启用了:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
我收录了3个文件。
<script src="~/Scripts/assets/plugins/jquery/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
我甚至将Layout设置为null以确保没有其他js文件正在中断。
<div id="contactSection">
</div>
@using (Ajax.BeginForm("SendContact", "Home", new AjaxOptions {UpdateTargetId = "contactSection" }))
{
<fieldset>
<div class="row">
<div class="form-group">
<div class="col-md-4">
<label >Full Name *</label>
@Html.TextBoxFor(m => m.contactVM.fullName, new { @class = "form-control", })
</div>
<div class="col-md-4">
<label >E-mail Address *</label>
@Html.TextBoxFor(m => m.contactVM.email, new { @class = "form-control", type = "email" })
</div>
<div class="col-md-4">
<label >Phone</label>
@Html.TextBoxFor(m => m.contactVM.phone, new { @class = "form-control" })
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-8">
<label >Subject *</label>
@Html.TextBoxFor(m => m.contactVM.subject, new { @class = "form-control" })
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label >Message *</label>
@Html.TextAreaFor(m => m.contactVM.message, new { @class = "form-control", maxlength = "10000", rows = "8" })
</div>
</div>
</div>
</fieldset>
<div class="row">
<div class="col-md-12">
<input type="submit" class="btn btn-primary">
</div>
</div>
}
在控制器中
[HttpPost]
public PartialViewResult SendContact(MainVM vModel)
{
vModel.contactVM.success = true;
return PartialView("_ContactResults", vModel.contactVM);
}
最后是部分视图
@if (Model.success)
{
<!-- Alert Success -->
<div id="alert_success" class="alert alert-success margin-bottom-30">
<button type="button" class="close" data-dismiss="alert" aria- hidden="false">×</button>
<strong>Thank You!</strong> Your message was successfully sent! As long it was not spam, we will get back to you asap.
</div><!-- /Alert Success -->
}
else
{
<!-- Alert Failed -->
<div id="alert_failed" class="alert alert-danger margin-bottom-30">
<button type="button" class="close" data-dismiss="alert" aria- hidden="false">×</button>
<strong>[SMTP] Error!</strong> Sorry something went wrong, try again please!
</div><!-- /Alert Failed -->
}
有什么想法吗?
答案 0 :(得分:0)
修正了问题。 在控制台中我收到此错误 - 未捕获的TypeError:a(...)。live不是函数。 据我记得,jquery中的live函数很久以前就被弃用了。 当我最初安装nuget包安装包Microsoft.jQuery.Unobtrusive.Ajax它安装版本2.0.20710 - 这是相当老的。我必须重新安装nuget包并强制它下载版本3.2.3