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

  • Home
  • SEARCH
  • 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 9141991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:43:06+00:00 2026-06-17T09:43:06+00:00

I am new to MVC4. I have to create a login name validation. After

  • 0

I am new to MVC4. I have to create a login name validation. After writing a string, when we exit from the textbox, it should display whether it is available or not.

The View Code is:

@{
    ViewBag.Title = "Home Page";
}
@section featured {
    <section class="featured">
        <div class="content-wrapper">
            @Html.TextBox("textbox1")
            @Html.TextBox("txtTest")
        </div>
    </section>
}
@section scripts{
    <script type="text/javascript">
        $(document).ready(function(){
            $('#textbox1').blur(function(){
                alert("a");
            });
        });
    </script>
}

Now in place of alert("a"), I will have to call an action. That action will contains the database check.

Controller Code:

public class HomeController : Controller
    {
        public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }
        public ActionResult SearchUser()
        {
            string ExistsCheck;
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"].ToString());
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand();
            DataTable dt = new DataTable();
            cmd = new SqlCommand("sp_UserName_Exist_tbl_UserDetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", Request.Form["textbox1"]);
            da.SelectCommand = cmd;
            da.Fill(dt);
            if (dt != null && dt.Rows.Count > 0 && dt.Rows[0][0].ToString().ToLower() == "exists")
            {
                ExistsCheck = "Exists";
            }
            else
            {
                ExistsCheck = "Available";
            }
            return View();
        }
    }

Now my question is how to call this SearchUser() action and display it into the same page when we go out from the textbox1.

Any suggestion please.

  • 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-17T09:43:07+00:00Added an answer on June 17, 2026 at 9:43 am

    JavaScript

    @{
        ViewBag.Title = "Home Page";
    }
    @section featured {
        <section class="featured">
            <div class="content-wrapper">
                <table>
                    <tr>
                        <td>
                            @Html.TextBox("textbox1")
                        </td>
                        <td>
                            <div id="regTitle"></div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            @Html.TextBox("txtTest")
                        </td>
                    </tr>
                </table>
            </div>
    
        </section>
    }
    @section scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $('#textbox1').blur(function () {
                var params = { userName: $(this).val() };
                $.ajax({
                    url: "Home/SearchUser",
                    type: "get",
                    data: { userName: $("#textbox1").val() },
                    success: function (response, textStatus, jqXHR) {
                        if (response.IsExisting) {
                            // User name is existing already, you can display a message to the user
                            $("#regTitle").html("Already Exists")
                        }
                        else {
                            // User name is not existing
                            $("#regTitle").html("Available")
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert("error");
                    },
                    // callback handler that will be called on completion
                    // which means, either on success or error
                    complete: function () {
                        }
                });
            });
        });
    </script>
    }
    

    Controller method

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace Mvc4_Ajax.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
    
                return View();
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "Your app description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
            [HttpGet]
            public ActionResult SearchUser(string userName)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"].ToString());
                SqlDataAdapter da = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand();
                DataTable dt = new DataTable();
                cmd = new SqlCommand("sp_UserName_Exist_tbl_UserDetails", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@UserName", userName);
                da.SelectCommand = cmd;
                da.Fill(dt);
                var isExisting = dt != null && dt.Rows.Count > 0 && dt.Rows[0][0].ToString().ToLower() == "exists";
                return Json(new { IsExisting = isExisting }, JsonRequestBehavior.AllowGet);            
            }
        }
    }
    
    • I would recommend using an ORM (Entity Framework or Nhibernate)
    • Beware SQL injection even if you’re using a stored procedure:
      http://www.troyhunt.com/2012/12/stored-procedures-and-orms-wont-save.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a method using the new WebAPI features in MVC4 and have it
If you create a new MVC4 web application with the Internet Application Project Template
New to PHP and MySQL, have heard amazing things about this website from Leo
I am new to MS's MVC4, and I have been given a database for
I am learning ASP.NET MVC4 Web APIs. I would like to create a new
In MVC4, if I create a new build configuration for all projects in a
I was attempting to create a new project in VS2012 with an Administrative MVC4
When I create a new razor view in ASP.NET MVC4, the title is automatically
I installed the new ASP .Net MVC4 beta on my machine and have been
New to SPA MVC4, trying to pass a session variable to LinqToEntitiesDataController from the

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.