MVC4を初めて使用しています。私はログイン名の検証を作成する必要があります。文字列を書き込んだ後、テキストボックスを終了すると、文字列が利用可能かどうかが表示されます。コードの表示があるMVC4でajaxコールを作成する方法
:
@{
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>
}
今alert("a")
の代わりに、私は、アクションを呼び出す必要があります。そのアクションにはデータベースチェックが含まれます。
コントローラコード:
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();
}
}
今、私の質問は、このSearchUser()
アクションを呼び出すと、我々はTextBox1テキストボックスから出て行くときに、同じページにそれを表示する方法です。
ご提案ください。私がORM(Entity FrameworkのかNHibernateの)を使用することをお勧めします
ビューをロードするときに初めてajaxが起動します。次回は、アヤックスは発砲していません。 –
あなたの例で* AJAXコードが表示されません。 –
JavaScriptコードは 'alert(" a ")のみを置き換えるものとしています。 – Gabriel