ASP.NET Core和JQuery不引人注目的AJAX无法正常工作 [英] ASP.NET Core and JQuery unobtrusive AJAX not working
问题描述
如果我在.net 4.5中启动一个新项目,然后单击Manage nuget软件包,则搜索ajax,然后单击install以确保ajax不引人注目.然后,我可以进入我的cshtml文件并输入@Ajax.___例如@ Ajax.beginForm
If I start a new project in .net 4.5 and hit manage nuget packages, search for ajax, then hit install for ajax unobtrusive. I can then go into my cshtml file and type @Ajax.___ e.g. @Ajax.beginForm
如果我使用Dot Net Core创建一个新项目...我不能,而是给了我无用的错误:
If I create a new project with Dot Net Core... I cannot, where instead it gives me the useless error:
名称'Ajax'在当前上下文中不存在"
"The name 'Ajax' does not exist in the current context"
我在网上搜索,什么都没找到...
I've searched the web, found nothing...
基本上,我想像在.net中那样用Ajax调用来创建表单
Basically I want to make a Form with an Ajax call as you could do in .net
例如:
"@using (Ajax.BeginForm("EmployeeMaster", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp" })) {
"
有人知道我还能尝试什么吗?
Anyone know what else I can try?
在上面,我可以看到Ajax是AjaxHelper类型的对象,它来自System.web.mvc.webviewpage ...因此,也许它永远都不意味着可用于.net core
In the above, I can see that Ajax is a object of type AjaxHelper which comes from System.web.mvc.webviewpage... so maybe its never meant to be available for .net core
推荐答案
虽然Ajax.BeginForm()
方法不起作用,但实际的Ajax html却不起作用. C#方法仅有助于生成HTML表单标签.原谅我这些语法是错误的,这是从内存中获取的,但是您知道了
While the Ajax.BeginForm()
methods do not work, the actual unobtrusive Ajax html does. The C# methods just help generate the Html form tags. Forgive me in thse syntax is wrong, this is from memory, but you get the idea
@using (Ajax.BeginForm("EmployeeMaster", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp" }))
{
//form content
}
成为
<form asp-action="EmployeeMaster" asp-controller="Home" method="POST" data-ajax="true" data-ajax-update="divEmp">
<!-- form content-->
</form>
注意::上述HTML STILL需要使用不引人注目的ajax js文件,以便其正确连接这些data- *属性.这不是ASP.NET Core MVC的一部分
NOTE: The above HTML STILL needs the unobtrusive ajax js files for it to wire up these data-* attributes correctly. This is not a part of ASP.NET Core MVC
这篇关于ASP.NET Core和JQuery不引人注目的AJAX无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!