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.
JavaScript
Controller method
http://www.troyhunt.com/2012/12/stored-procedures-and-orms-wont-save.html