2

I have a model which has Required attributes in it. Is it possible to do data validation on a button click which is not of type submit. I did refer to this article and this one but it did not solve the issue.

Here's my model code.

 [Required]
 [Display(Name = "Number of beds")]
 public int Beds { get; set; }
 [Required]
 [Display(Name = "Number of Inpatient Beds")]
 public int InpatientBeds { get; set; }

My View

@using (Html.BeginForm(new {id = "form1", @class = "form-horizontal" }))
{

<div class="divPanel">
    <div class="row">
        <div class="col-md-3">                
                @Html.LabelFor(m => m.Beds)
                @Html.TextBoxFor(m => m.Beds, new { @class = "form-control", @type = "number"})
                @Html.ValidationMessageFor(m => m.Beds)

        </div>
        <div class="col-md-3">

                @Html.LabelFor(m => m.InpatientBeds)
                @Html.TextBoxFor(m => m.InpatientBeds, new { @class = "form-control", @type = "number"})
                @Html.ValidationMessageFor(m => m.InpatientBeds)
        </div>
<div class="col-md-3">             
            <button type=button  class="btn btn-primary" id="btnCalculateScore" > Calculate Score</button>
        </div>
 </div>
<div class="col-md-3">           
        <button type="submit" class="btn btn-primary" id="btnSaveChanges" data-toggle="modal"><i class="fa fa-share-square-o"></i> Update Status</button>
    </div>
</div>
}

And my script

$('#btnCalculateScore').on('click', function (evt) {

evt.preventDefault();

if ($('#form1').valid()) {
    //Do Something.
}
}

The validations do not work and the control enters the if loop even though the fields are empty? Is there something I am missing? Also does this approach also give out the validation messages which normally happen on a submit button event?

EDIT :

  1. I added a normal Update Status button with type submit and the validations work there fine.

  2. I added the id to my form to ensure it does not matter if the View contains another forms.

7
  • Does client-side validation work if you change button type to type="submit"? I just want to know jquery.validate.unobtrusive works in your code. Commented Mar 30, 2017 at 16:44
  • @Win I did change it to submit and used jquery.validate.unobtrusive. But still does not seem to validate properly Commented Mar 30, 2017 at 16:55
  • Did you included the jquery libraries? Commented Mar 30, 2017 at 16:55
  • @CristianSzpisjak I did. Else I would have got an error on the button click itself. Commented Mar 30, 2017 at 16:59
  • 1
    You do not have a form with id="form1" - your overload of BeginForm() is adding route values, not html attributes. Commented Mar 31, 2017 at 22:57

1 Answer 1

0

I had the same issue and this seemed to resolve it for me.

@using (Html.BeginForm(new {id = "form1", @class = "form-horizontal" }))

On seeing the page source, this code does not attach the id Form1 to your form. Hence your validation will not happen as the form with id form1 is not found.

Instead,assuming your operation is a post and you are saving the data in say Action Method Save and controller Bed you can edit the form to

@using (Html.BeginForm("Save", "Bed", FormMethod.Post, new { id = "form1",@class = "form-horizontal"}))

This will attach the id parameter to your form(you can check using ViewPageSource) and it should work. There might be a better solution to this, but this worked properly for me without issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.