Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:02:11+00:00 2026-06-04T02:02:11+00:00

How can I return custom validation messages using JsonResult and AJAX? Here is my

  • 0

How can I return custom validation messages using JsonResult and AJAX?

Here is my controller action to add a student in StudentDB.

UPDATED:

    [HttpPost()]
    public ActionResult AddStudent(string studentName, int studentId)
    {
        var studentPresent = studentTable.Students.Where(s => s.StudentID == studentId&& b.StudentName == studentName);

        if (studentPresent == null || !studentPresent .Any())
        {
            var student = new Student()
                {
                    StudentName = studnetName,
                    StudentID = studentId,
                };
            studentTable.AddObject("Student", student);
            studentTable.SaveChanges();

         }
        return new JsonResult();      

    }

Here is my JavaScript:

function addStudent() {
    $.ajax({
        type: 'POST',
        url: '/StudentAdmin/AddStudent',
        data: {
            studentName: $('#studentName').val(),
            studentNumber: GetTextBoxValue('#studentNumber'),            
        },
        success: function (result) {
            if ($('#studentPresent').val() == null) {
            showMessage('Success', 'Student saved successfully.', '', false);                
        } else {
            showMessage('Error', 'Student already present in database.', '', false);
        }
            GetGrid('#studentGrid').ajaxRequest();
            hideForm();
        },
        studentPresent: function (result) {
            showMessage('Error', 'Student Already present in Database.', '', true);
        }

    });
}

I want to display the “error” message if this student is already present in database.
Also, Is there a way of passing more validation messages to JasonResult?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:02:12+00:00Added an answer on June 4, 2026 at 2:02 am

    You can pass any object to the JsonResult object and it will be serialized (or it will attempt to serialize) down to the javascript.

    return new JsonResult(new { Anything = "Hello World" });
    

    That results in a JSON object like so:

    {"Anything":"Hello World"}
    

    Being rendered to your javascript in the result variable.

    Your code above actually doesn’t show any error message being generated; you would need a try / catch block if you want to get the text of the SQL exception.

    EDIT:

    You would have code like so:

    return Json(new { message = "Success" }); // success message
    return Json(new { message = "Failure" }); // fail message
    

    And then in javascript, your callback is like so:

    success: function(result)
    {
         if(result.message == "Success")
           // Show success message
         else
           // Show Error Message
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Validation Rules to show custom messages, and INotifyDataError for the business rules.
Im using jQuery's validation which can be found here: http://docs.jquery.com/Plugins/Validation/rules#.22add.22rules I currently have some
How can i get IIS 7 to return my custom error pages for file
In c# you can return an integer to the underlying caller using Environment.Exit(n) (which
In my controller I have model operations that can return empty results. I've setup
I'm wondering how one can catch and add a custom handler when empty results
I am using CodeIgniter's form validation . Here is an example: $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
How can I return errors messages from a cross reference table with multiple records
I'm trying to create a custom validation method in JQuery and I can't manage
I wrote a custom validation handler for this ASP.NET MVC application. Here is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.