MVC中提交表单会有成功后执行前端js的OnSuccess = "doResult"
@using (Ajax.BeginForm("ModifyPhoneOverseas", "ResumeUpdate", FormMethod.Post, new AjaxOptions { OnSuccess = "doResult" }))
{
}
在NET CORE里面new AjaxOptions { OnSuccess = "doResult" })又该怎么样写呢?
MVC中提交表单会有成功后执行前端js的OnSuccess = "doResult"
@using (Ajax.BeginForm("ModifyPhoneOverseas", "ResumeUpdate", FormMethod.Post, new AjaxOptions { OnSuccess = "doResult" }))
{
}
在NET CORE里面new AjaxOptions { OnSuccess = "doResult" })又该怎么样写呢?
在ASP.NET Core中已经不再使用Ajax.BeginForm
,而是使用Html.AjaxBeginForm
。
完整的流程:
1.用以下命令安装包:
PM> Install-Package AspNetCore.Unobtrusive.Ajax
2.将这个包注册到Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddUnobtrusiveAjax();
//services.AddUnobtrusiveAjax(useCdn: true, injectScriptIfNeeded: false);
//...
}
public void Configure(IApplicationBuilder app)
{
//...
app.UseStaticFiles();
//It is required for serving 'jquery-unobtrusive-ajax.min.js' embedded script file.
app.UseUnobtrusiveAjax(); //It is suggested to place it after UseStaticFiles()
//...
}
3.把js文件加到_Layout.cshtml里面:
<!--Place it at the end of body and after jquery-->
@Html.RenderUnobtrusiveAjaxScript()
<!-- Or you can reference your local script file -->
@RenderSection("Scripts", required: false)
</body>
</html>
4.至于你所说的想知道如何实现new AjaxOptions { OnSuccess = "doResult" })
,跟MVC是一样设置的,在ASP.NET Core中设置:
@{
AjaxOptions ajaxOptions = new AjaxOptions
{
HttpMethod = "POST",
OnSuccess="doResult"
//other options....
};
}
@using (Html.AjaxBeginForm("ActionName", "ControllerName", ajaxOptions))
{
//other elements...
<input type="submit" value="Submit Details" />
}
//....