Brendan Vogt
Brendan Vogt

Reputation: 26028

Using jQuery Validate to validate and to do an AJAX call to return partial views

I am using ASP.NET MVC 4 and jQuery Validate.

I have a textbox and a button on my page. It is a button and NOT a submit button. When the button is clicked I validate the number entered into the textbox and display 1 of 2 partial views.

Here is my HTML markup:

@Html.TextBoxFor(x => x.ChangeIncidentNumber)
<button id="verifyButton" type="button">Verify</button>
@Html.ValidationMessageFor(x => x.ChangeIncidentNumber)

Before I started using jQuery Validate I did it like this:

$(document).ready(function () {
     $('#verifyButton').click(function () {
          $.ajax(
          {
               type: "POST",
               url: "/Server/GetChangeIncidentDetails",
               data: { changeIncidentNumber: $('#ChangeIncidentNumber').val() },
               dataType: "html",
               success: function (result) {
                    var domElement = $(result);
                    $("#changeIncidentDetails").html(domElement);
               }
          });
     });
});

My GetChangeIncidentDetails action method:

public ActionResult GetChangeIncidentDetails(string changeIncidentNumber)
{
     ImplementationType implementationType = GetImplementationType(changeIncidentNumber);

     try
     {
          if (implementationType == ImplementationType.Change)
          {
               Change change = changeService.FindChangeByNumber(changeIncidentNumber);
               if (change != null)
               {
                    ChangeViewModel viewModel = Mapper.Map<ChangeViewModel>(change);

                    return PartialView("ChangeInformation", viewModel);
               }
               else
               {
                    // If it is null then I want to display an error message on the front end
               }
          }
          else if (implementationType == ImplementationType.Incident)
          {
               // Code is almost similar as above
          }
          else
          {
          }
     }
     catch (Exception ex)
     {
          // Handle error
     }
}

I added jQuery Validate to the picture to handle some client side validation before I go into the action method above. This is what I currently have:

$(document).ready(function () {
     $("#searchForm").validate({
          rules: {
               ChangeIncidentNumber: "required"
          },
          messages: {
               ChangeIncidentNumber: "Required"
          }
     });

     $('#verifyButton').click(function () {
          $('#searchForm').valid();
     });
});

I'm not sure how to merge the 2 together, can the validation and the ajax call be done together, do I need to keep it separate, and how do I display a client side error message if change == null?

Upvotes: 3

Views: 340

Answers (1)

Daniel Powell
Daniel Powell

Reputation: 8293

I believe you can do:

  $('#verifyButton').click(function () {
          $('#searchForm').validate();

          if($('#searchForm').valid()){    
              $.ajax({
                   type: "POST",
                   url: "/Server/GetChangeIncidentDetails",
                   data: { changeIncidentNumber: $('#ChangeIncidentNumber').val() },
                   dataType: "html",
                   success: function (result) {
                       var domElement = $(result);
                       $("#changeIncidentDetails").html(domElement);
                       }
              });
          }
     });

Note I didnt check the syntax exactly might have an extra or missing } or )

To return an error you can return a json object from your action method that looks something like

{success:true/false, 
 data:"jsondatafromactionmethod",
 error:"errormessagehere"}

If the success is true you do the html replace/insert otherwise you show the error any way you want.

Upvotes: 1

Related Questions