0

View -

@using (Html.BeginForm("MethodName", "Build"))
{
    <div>
        @Html.TextAreaFor(model => model.OwnerComments, new { @id = "txtComments", @class = "form-control", @TextMode = "MultiLine", width = "400px" ,maxlength = "300" })
        @Html.ValidationMessageFor(model => model.OwnerComments, "", new { style = "color:red;" })
    </div>
    <input type="submit" id="btnSave" class="but_4 btn btn-primary" value="Save" onclick="ShowErrors();" />
}

When i keep my textarea blank, it shows the validation message yet it calls the action -"MethodName" immediately Note - I have put [Required] attribute to my OwnerComments property

mmushtaq
  • 3,430
  • 7
  • 30
  • 47
  • What is your `onclick="ShowErrors()`? And what validation attributes are applied to `OwnerComments`? And have you included the relevant jquery validation scripts in the view? –  Jul 15 '16 at 09:29
  • I have applied [Required(ErrorMessage = "Please enter comments")] to my OwnerComments. And ShowError is a function which just shows the validation message that i am hiding previously – Prajwal Deshmukh Jul 15 '16 at 09:31
  • Yes both the jQuery validation scripts are there. It is validating perfectly but even after validating it is calling the Action – Prajwal Deshmukh Jul 15 '16 at 09:32
  • 1
    And what is your `ShowErrors()` function? –  Jul 15 '16 at 09:33
  • ShowErrors - function ShowErrors() { $(".field-validation-error").show(); } – Prajwal Deshmukh Jul 15 '16 at 09:33
  • Why in the world would you need that (its not hidden)? –  Jul 15 '16 at 09:34
  • I hide those when i open the popup. I show them again when the Save button is clicked. But even after removing those functions its not making any difference – Prajwal Deshmukh Jul 15 '16 at 09:37
  • Why? (nothing will be visible in the validation message when its first displayed). And what popup are you referring to. You need to edit you question and include ALL the relevant information. Best guess is your dynamically loading a form into the view –  Jul 15 '16 at 09:43
  • I agree the information regarding that javascript function and popup is not given but forget about that. I removed that function and yet the problem is there. This isn't because of that function. The problem is that even after validation the action is getting called which should not happen. Might be because of some script is failing somewhere. I will post the solution once I find it – Prajwal Deshmukh Jul 15 '16 at 09:56

3 Answers3

0

Please do check your web.config and make sure that following app setting must be present in it to check validation at client-side.

 <appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
mmushtaq
  • 3,430
  • 7
  • 30
  • 47
  • 1
    All these required steps are there. Surprisingly it is working in another View but not working in this View. Layout is same for both the views, scripts are same even code is same but it is not working for this one – Prajwal Deshmukh Jul 15 '16 at 09:50
0

You need to add following script in your .cshtml/View Page:

<script src="~/scripts/jquery.validate.unobtrusive.min.js"></script>

in web.config Please add this:

<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

Hope it solves your problem :)

Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
0

Solved - This issue was with jquery.validate.unobtrusive.js file. As of jQuery 1.9, the behavior of parseJSON() has changed and an undefined value would be considered a malformed JSON, resulting in the error i've specified. Refer this - Syntax error with parseJSON during unobtrusive validation Need to add below reference to resolve theis issue http://blog.jquery.com/2013/01/31/jquery-migrate-1-1-0-released/

Community
  • 1
  • 1