在浏览器控制台中找不到元素:MVC远程验证 [英] Element not Found in Browser Console :MVC remote validation
问题描述
我正在使用远程验证开发MVC应用程序。我在浏览器控制台中收到No element found错误。我得到了很多Jquery未找到的错误,我可以通过呈现所需的脚本来删除。现在我在浏览器控制台中只有一个错误。
我的浏览脚本:放在页脚中
I am developing a MVC application with Remote validations. I am getting a No element found error in the browser console. I was getting so many Jquery not found errors that I could manage to remove by rendering the required Scripts. Now I have only one error in the browser console.
My View Script : Placed in the footer
@section Scripts {
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/scripts/jquery-1.10.2.js")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.validate.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/jquery-1.10.2.min.js")
@Scripts.Render("~/Scripts/jquery-ui-1.11.4.min.js")
@Scripts.Render("~/~/Scripts/jquery.unobtrusive-ajax.js")
}
布局:在身体之前(根据建议我阅读)
In layout : Before the body (based on a suggestion I read)
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
<script src="@Url.Content("~/Scripts/jquery-ui-1.11.4.min.js")"></script>
我怎样才能使这个工作?我使用了远程验证,我想我的代码中缺少用于验证的Jquery。但是我已经添加了我所遵循的远程验证教程中提到的所有脚本文件(虽然我添加了更多)。
远程验证
How can I get this working? I have used remote validation and I suppose the Jquery used for the validation is missing in my code. But I have added all the Script files that was mentioned in the Remote validation tutorial I followed (I have added more though).
Remote Validation
[HttpPost]
public JsonResult DuplicateFamilyName(string FamilyName, int FamilyID)
{
//bool idExists = db.LsystemFamily.Any(id=>id.LsystemFamilyID.Equals(FamilyID));
if (FamilyID == 0)
{
bool exists = db.LsystemFamily.Any(x => x.FamilyName == FamilyName);
//var name = db.LsystemFamily.Where(x => x.FamilyName.Equals(FamilyName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
return Json(!exists, JsonRequestBehavior.AllowGet);
}
else
{
bool exists = db.LsystemFamily.Where(x => x.LsystemFamilyID != FamilyID).Any(x => x.FamilyName == FamilyName);
//var name = db.LsystemFamily.Where(x => x.FamilyName.Equals(FamilyName, StringComparison.CurrentCultureIgnoreCase) && x.LsystemFamilyID != FamilyID).FirstOrDefault();
return Json(!exists, JsonRequestBehavior.AllowGet);
}
}
编辑
Edit
public class BundleConfig
{
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
bundles.Add(new ScriptBundle("~/bundles/jqueryajax").Include(
"~/Scripts/jquery.unobtrusive*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
}
}
推荐答案
你已经在_Layout中加载了JQuery。从你的视图中取出所有这些脚本调用,除了:
@ Scripts.Render(〜/ bundles / jqueryval)
多次调用Javascript文件通常会破坏浏览器的功能。你在unobtrusive-ajax上的路径也是没有用的。
如果你发布你的App_Start / BundleConfig.cs我们可以确保你拥有所有的你需要的脚本。
另外,看看网页优化的Asp.Net入门:
http://www.asp.net/mvc/overview/performance/bundling-and-minification [ ^ ]
You already have JQuery loaded in your _Layout. Take all of those script calls out of your view, except for:
@Scripts.Render("~/bundles/jqueryval")
Calling a Javascript file more than once will often break functionality on the browser. It also doesn't help that your path on unobtrusive-ajax is wonky.
If you post your App_Start/BundleConfig.cs we can make sure that you have all the scripts that you need in there.
Also, have a look at the Asp.Net primer on web Optimization:
http://www.asp.net/mvc/overview/performance/bundling-and-minification[^]
这篇关于在浏览器控制台中找不到元素:MVC远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!