Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
@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)


HTML
<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>


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

C#
[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

C#
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"));

    }
}
Posted
Updated 7-Oct-15 21:49pm
v3

1 solution

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[^]
 
Share this answer
 
Comments
vini vasundharan 6-Oct-15 3:14am    
Yes. I removed the Scripts in my View and it worked. But i could not get my Json validation working.
Nathan Minier 7-Oct-15 7:15am    
Okay. Please put your BundleConfig.cs file up so that we can work it through.
vini vasundharan 8-Oct-15 3:50am    
Added
Nathan Minier 8-Oct-15 7:23am    
Okay, make these changes to the Bundles:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-ui-1.11.4.min.js",
"~/Scripts/jquery.unobtrusive*"));

This will give you access to the jQueryUI components and AJAX without a separate bundle call.

Now, providing that your Model is marked up correctly you should have validation. Attributes such as [Required] or [RegEx()] can be added to the model, and those will translate over to to the Razor providing you use a format that supports is, such as @Html.EditorFor.

You'll need to add a @HtmlValidationMessageFor on the oitems that will show validation, or make sure that you have a @Html.ValidationSummary to provide the feedback.

There's a decent basic validation tutorial at:
http://www.asp.net/mvc/overview/getting-started/introduction/adding-validation

vini vasundharan 8-Oct-15 8:37am    
I am having remote validation that is not working for. the normal required attribute is working. ihave also checked with validation in the controller and that is laso working fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900