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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:34:30+00:00 2026-06-15T19:34:30+00:00

I am writing some code where the user can check if a username already

  • 0

I am writing some code where the user can check if a username already exists in the database before submitting the form. This works by on onkeyup event handler which takes the value from the textbox and passing it to an ajax request which calls my ActionResult method with a string parameter. My SQL logic works okay, I recieve a value which says already exists or not.

The problem I have is from my Controller, I want to return the value. In the success part of my Jquery Ajax request I am trying to access my ViewData[“index name”] but my alert shows an empty string.

Javascript code :

function searchUsername() {
     var filters = getFilterVals();
     $.ajax({
         url: '@Url.Action("UsernameSearch", "UserManager")',
         type: "POST",
         async: true,
         dataType: "text",
         data: "username=" + filters.username,
         success: function () {
             var str = '@ViewData["UserName"]';
             alert(str);
             $('#txtUsernameExists').val(str);
             configureGui();
             jQueryTableStyling();
         },
         error: function (data) {

         }
     });
}

function getFilterVals() {
     filters = new Object();
     filters.username = $('#createuser-usernamesearch #user_name').val();
     return filters;
}

C# controller code :

public ActionResult UsernameSearch(string username)
{
    string output = DAL.usernameSearch(username);
    ViewData["Username"] = output;
    return PartialView("~/Views/Partial/_txtUsernameSearch.cshtml");
}

What am I doing wrong? Any suggestions are welcome.

  • 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-15T19:34:31+00:00Added an answer on June 15, 2026 at 7:34 pm

    ASP.NET:

    public JsonResult UsernameSearch(string username)
    {
                string output = DAL.usernameSearch(username);
    
                return Json(new
                {
                    Html = PartialView("~/Views/Partial/_txtUsernameSearch.cshtml"),
                    Username = output
                });
    }
    

    JS:

    $.ajax({
                url: '@Url.Action("UsernameSearch", "UserManager")',
                type: "POST",
                async: true,
                dataType: "json",
                data: "username=" + filters.username,
                success: function (data) {
    
                    var html = data.Html;
                    var str = data.Username;
                    alert(str);
    
                    $('#txtUsernameExists').val(str);
                    configureGui();
                    jQueryTableStyling();
                },
                error: function (data) {
    
                }
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get some better logic behind this before I start writing code.
I'm writing some web UI pages that can be used to create Linux user
I'm writing some form. I have one field in this form that contains a
I can use GD or something to draw user-selected shapes by writing some lines
I am re-writing some code for a new re-design and I am wondering if
I'm writing some code that will take a screenshot of another application, given its'
I'm writing some code for a very limited system where the mod operator is
I am writing some code in C: int main(){ char guess[15]; guess = helloValidation(*guess);
I am writing some code that will go through a file, edit it as
I am writing some code where I count the number of Div children and

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.