如何在C#mvc中键入文本框时检查用户名可用性 [英] how to check username availability while typing in textbox in C# mvc
本文介绍了如何在C#mvc中键入文本框时检查用户名可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码和我必须做的事情
C#
public bool IsInvoiceExists( string invoicenumber)
{
尝试
{
string connstring = Data Source = .; Initial Catalog = Accounts; Integrated Security = True;
string query = 从AccountSummary中选择*其中InvoiceNo = @ InvoiceNo;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(query,conn);
cmd.Parameters.AddWithValue( @ InvoiceNo,invoicenumber);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt == null || dt.Rows.Count == 0 )
{
bool isExist;
isExist = db.Users.Any(u = > u.Name == userName);
return Json(isExist);
}
其他
{
返回 假跨度>;
}
}
catch (例外)
{
掷跨度>;
}
}
jquery
$( #InvoiceNo)。keyup( function (){
$ .ajax({
url: @ Url.Action ( IsInvoiceExists , Home ),
data:$( this )。val(),
类型: GET,
cache: false ,
成功: function (数据){
if (数据){
$( #errorMessage)。text( Invoice Exist);
}
else {
$( #的errorMessage跨度>)空();
}
},
错误:功能(xhr){
}
});
});
解决方案
( #InvoiceNo)。keyup( function (){
.ajax({
url: @ Url.Action( IsInvoiceExists ,主页 ),
data:
( this )。val(),
type:< span class =code-string> GET,
cache: false ,
成功: function (data){
if (data) {
Here is my code and what i have to do
C#
public bool IsInvoiceExists(string invoicenumber)
{
try
{
string connstring = "Data Source=.;Initial Catalog=Accounts;Integrated Security=True";
string query = "select * from AccountSummary where InvoiceNo=@InvoiceNo";
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@InvoiceNo", invoicenumber);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt == null || dt.Rows.Count == 0)
{
bool isExist;
isExist = db.Users.Any(u => u.Name == userName);
return Json(isExist);
}
else
{
return false;
}
}
catch (Exception)
{
throw;
}
}
jquery
$("#InvoiceNo").keyup(function () {
$.ajax({
url: "@Url.Action("IsInvoiceExists", "Home")",
data: $(this).val(),
type: "GET",
cache: false,
success: function (data) {
if (data) {
$("#errorMessage").text("Invoice Exist");
}
else {
$("#errorMessage").empty();
}
},
error: function (xhr) {
}
});
});
解决方案
("#InvoiceNo").keyup(function () {
.ajax({ url: "@Url.Action("IsInvoiceExists", "Home")", data:
(this).val(), type: "GET", cache: false, success: function (data) { if (data) {
这篇关于如何在C#mvc中键入文本框时检查用户名可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文