-1

I am new to MVC and I am facing very foolish problem in my project, I read that the model manages client side validation in MVC using Jquery, so I have already added few required jquery in my Bundle. Config File.Even though at the time of validation the page gets Post backs, even if the validation got fired, as I read it should not postback if the violation of validation is occurred, please help me to resolve this problem.

Below is the snippet of Bundle.Config File. Please check am I missing something?

 using System.Web;
  using System.Web.Optimization;

namespace DesignationDemo
{
    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrapjs").Include(
                        "~/Scripts/bootstrap.min.js"));

            bundles.Add(new StyleBundle("~/Content/bootstrapcss").Include(
                        "~/Content/bootstrap.min.css",
                        "~/Content/bootstrap-responsive.min.css"));


            // 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 StyleBundle("~/Content/css").Include("~/Content/site.css"));


bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
              "~/Content/themes/base/jquery.ui.core.css",
              "~/Content/themes/base/jquery.ui.resizable.css",
              "~/Content/themes/base/jquery.ui.selectable.css",
              "~/Content/themes/base/jquery.ui.accordion.css",
              "~/Content/themes/base/jquery.ui.autocomplete.css",
              "~/Content/themes/base/jquery.ui.button.css",
              "~/Content/themes/base/jquery.ui.dialog.css",
              "~/Content/themes/base/jquery.ui.slider.css",
              "~/Content/themes/base/jquery.ui.tabs.css",
              "~/Content/themes/base/jquery.ui.datepicker.css",
              "~/Content/themes/base/jquery.ui.progressbar.css",
              "~/Content/themes/base/jquery.ui.theme.css"));



        }
    }
}

View for "Create" operation

@using (Html.BeginForm("Create", "DesignationInfo", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{  
    @Html.ValidationSummary(true)  

    <table>

        <tr> <td>
           <b>Designation Name  </b>
        </td>
        <td>
            @Html.EditorFor(model => model.Name)  
            @Html.ValidationMessageFor(model => model.Name)  
            </td>
        </tr>

        <tr> <td> 
          <b>  Active </b>
        </td>
       <td>
            @Html.CheckBoxFor(model => model.Active, new { @checked = "checked" })


        </td>
            </tr>
    <tr><td>
           </td>                             
             <td></tr>
         <tr> <td> </td><td>

                  <input type="submit" value="Save" class="btn btn-success" />  &nbsp;&nbsp;&nbsp;
             <input type="button" value="Cancel" onclick="Cancel()" class="btn btn-danger" />  &nbsp;&nbsp;&nbsp;
                <input type="button" value="Back" onclick="Close()" class="btn btn-warning" /></td>


        </tr>
    </table>
}  



 <script src="~/Scripts/bootstrap.min.js"></script>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />


  @Scripts.Render("~/bundles/jquery")    
@Scripts.Render("~/bundles/jqueryval")   
@Scripts.Render("~/bundles/jqueryui")


<script type="text/javascript">  

    function Close() {
        window.location.href = '/DesignationInfo/';

    }
    function Cancel() {
        $('#Name').val("")
        $('#Active').prop('checked', false);
    }
    </script>

Model as below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace DesignationDemo.Models
{

    public class Designation
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Can not be blank Name")]
        public string Name { get; set; }
        private bool myVal = true;
        [DefaultValue(true)]        
        public bool Active
        {
            get
            {
                return myVal;
            }
            set
            {
                myVal = value;
            }
        }

    }
}

Thanks in advance.

Sagar R
  • 595
  • 3
  • 14
  • Is client side validation enabled in you web.config file? Also use your browser tools in check that the scripts are actually being loaded in the view. –  Jan 12 '16 at 06:18
  • 1
    Based on your edit your have not included the `~/bundles/jqueryval` scripts in your view –  Jan 12 '16 at 06:35
  • Thanks @StephenMuecke : after your suggestion i have included that file too,but still postback occurs. i think there is some problem in bundle.config as this kind of problem also occurs in newly added pages too. – Sagar R Jan 12 '16 at 06:38
  • Edit your question to show where you have added it (it needs to be after `@Scripts.Render("~/bundles/jquery")`. And what is your triggering your `SaveDesignation` script? (and why do you even have it - what is the point of using ajax to post data to the controller, return a result and then redirect - you should just be doing a standard submit) –  Jan 12 '16 at 06:42
  • sir as per your suggestion i have updated my code. – Sagar R Jan 12 '16 at 06:50
  • That looks fine and should work assuming client side validation has not been disabled (unless the form has been dynamically loaded using javascript). If the `Name` textbox is left blank when you click the submit button, and error message will be displayed and submit will be cancelled –  Jan 12 '16 at 06:54
  • my web.config settings – Sagar R Jan 12 '16 at 06:56
  • Just check my updated answer, it seems you might miss to bind model with that view ... Not sure if it is the same problem ... – Mayank Parmar Jan 12 '16 at 07:03
  • Add @model DesignationDemo.Models.Designation at top of view ... – Mayank Parmar Jan 12 '16 at 07:04
  • @MayankParmar, If that were not already in the view, OP's code would be throwing errors :) - e.g. `@Html.EditorFor(model => model.Name)` –  Jan 12 '16 at 07:05
  • Right @StephenMuecke – Mayank Parmar Jan 12 '16 at 07:08
  • @MayankParmar : its been already added earlier,but did not disclose here,i can understand without that even i can't run a project,its mandatory so cannot considered in this scenario.the problem it that all things works fine except this client side validation (Validation also fires but after postback only) . – Sagar R Jan 12 '16 at 07:08
  • 1
    I just tested your code, both Model & View at my side ... It seems working fine ... I will soon upload my sample on Google Drive & share it to you ... May be you can compare my sample project & your project & find the difference ... If possible you may also upload your project solution & share it ... – Mayank Parmar Jan 12 '16 at 07:25
  • @StephenMuecke : by analyzing i found it puts the message for validation client submit even at client side but the thing is that its still postback which should not be done in this case. – Sagar R Jan 12 '16 at 07:26
  • thanks @MayankParmar, i would like to compare that code asap,so can resolve my problem soon. – Sagar R Jan 12 '16 at 07:27
  • 1
    Get it from [link](https://drive.google.com/file/d/0B-pLEe4JPEUpaGdrNzJreWNseFk/view?usp=sharing) – Mayank Parmar Jan 12 '16 at 07:40
  • Run & Go to - http://localhost:12122/Designation/Create - Click Save ... Validation Fires & no postback ... Port may be different in URL in your case – Mayank Parmar Jan 12 '16 at 07:40
  • @SagarRupani any updates ? Did it help ? – Mayank Parmar Jan 12 '16 at 09:14
  • Thanks you so much @MayankParmar your demo helped a lot for confirmation,at the conclusion what I found - > there were problem in Jquery (.JS) Files Resides in my machine,so what I did replace your .js file into my machine and now it works fine. – Sagar R Jan 12 '16 at 09:44
  • These files were responsible for autopostback so i replaced it with your .js file, jquery.unobtrusive-ajax.js jquery.validate.js jquery.validate.unobtrusive.js – Sagar R Jan 12 '16 at 09:45

1 Answers1

1

You may also try adding at top of view, it might fix the problem ....

@model DesignationDemo.Models.Designation

By adding in just bundles.config is not enough ...

You need to add below code in view also

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")

Also put some code of Model & View, so I can check more ...

Forgot to mention earlier ...

The most important code

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
Mayank Parmar
  • 164
  • 1
  • 7
  • Please check the question again i have added Model and View for your further analysis.. – Sagar R Jan 12 '16 at 06:31
  • I can't find @Scripts.Render("~/bundles/jqueryval"), can you check ? ... I modified my message, later ... I forgot it earlier ... – Mayank Parmar Jan 12 '16 at 06:45