Tôi mới tham gia MVC4. Tôi phải tạo xác nhận tên đăng nhập. Sau khi viết một chuỗi, khi chúng ta thoát khỏi hộp văn bản, nó sẽ hiển thị cho dù nó có sẵn hay không.Cách thực hiện cuộc gọi ajax trong MVC4
The View Code là:
@{
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>
}
Bây giờ ở vị trí của alert("a")
, tôi sẽ phải gọi một hành động. Hành động đó sẽ chứa kiểm tra cơ sở dữ liệu.
khiển 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();
}
}
Bây giờ câu hỏi của tôi là làm thế nào để gọi SearchUser()
hành động này và hiển thị nó vào trang tương tự khi chúng tôi đi ra khỏi TextBox1.
Bất kỳ đề xuất nào vui lòng.
Lần đầu tiên ajax kích hoạt khi tải chế độ xem. Lần sau, ajax không bắn. –
Tôi không thấy * bất kỳ mã AJAX nào trong ví dụ của bạn. –
Mã JavaScript của tôi được cho là chỉ thay thế 'alert (" a ");' bạn nên giữ mọi thứ khác. – Gabriel