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 8384691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:28:11+00:00 2026-06-09T17:28:11+00:00

I am working on a validation routine that needs to go back to the

  • 0

I am working on a validation routine that needs to go back to the server, perform a bit of database checking and then return to the client to display messages depending on the outcome.. I am using MVC3, Javascript, Ajax.

This is my clientside:

<script type="text/javascript">
$('#usrid').change(function (e) {

    var formData = $("form").serialize();

    $.ajax({
        url: "ValidateOfficers",
        type: "POST",
        data: formData,
        success: function (data) {
            alert(data.reponseText);
        },
        error: function (data) {
            alert(data.reponseText);
        }
    });
});

This is my controller action:

        [HttpPost, ActionName("ValidateOfficers")]
    public ActionResult ValidateOfficers(userRole role)
    {
        int holderid = role.holderid;
        int roleSelected = role.roleid;
        int chosenUser = role.usrid;
        int countRoles = db.userRoles.Where(i => i.roleid == roleSelected && i.holderid == holderid).Count();

        if (countRoles == 0) //success:
        {
            Response.StatusCode = 200;
            Response.Write("ok");
            return View("Create", role);
        }
        if (countRoles > 0) //error:
        {
            Response.StatusCode = 500;
            Response.Write("error");
            return View("Create", role);
        }
        return View();
    }

What I want to do perform the count and then send back a status based on the count result.. So if countRoles = 1 then return text “officer exists” if countRoles > 1 then “duplicate records exist”. if countRoles is zero then I can assume success and offer no message.

Ok, I am stuck on how to built this custom message on the server side and how to post it back to the client. I was thinking of adding a couple of labels to the razor and show/hiding depending on the count result.. As you can see I have had a very basic go, but to no avail. I get “undefined” in the alert messages currently.

If I can get some tips on this I would be most grateful!!

  • 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-09T17:28:12+00:00Added an answer on June 9, 2026 at 5:28 pm

    Action

    public ActionResult ValidateOfficers(userRole role)
    {
      string myMessage;
      \\ here goes all of your other code
      \\ set value into myMessage depending on your conditions.
    
      return Json(new {foo=myMessage});
    }
    

    Javascript

    <script type="text/javascript">
    $('#usrid').change(function (e) {
    
        var formData = $("form").serialize();
    
        $.ajax({
            url: "ValidateOfficers",
            type: "POST",
            data: formData,
            success: function (data) {
                alert(data.foo);
    
                if(data.foo == "exists")
                { // do something (show/hide msgs)
                }
                else if(data.foo == "duplicate")
                { // do something else (show/hide msgs)
                }
                else
                { // do something else (show/hide msgs)
                }
    
            },
            error: function (data) {
                alert(data.reponseText);
            }
        });
    });
    

    and you also would want to read this article, for knowing how to return json data.

    Hope this solves your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't seem to get client side validation working with the version of MVC
Client side Validation and ValidationSummary is working fine for my project (MVC4 + Razor
I have simple php validation form that is halfway working. If you leave the
I have fully working validation script my problem is that i can't get custom
I'm trying to get client validation working on my asp.net mvc 2 web application
I have basic client-side validation working in my MVC3 RC2 application, but I'm now
I am trying to get client validation working in MVC3 using data annotations. I
I can't seem to get any client side validation working on a MVC 2
I have noticed that my server-side receipt validation code is failing only for the
I have ajax requests that come into my controller and my validation is working

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.