繁体   English   中英

ASP.NET MVC注册警报SQL func用户存在

[英]ASP.NET MVC Registration alert SQL func user existence

我正在ASP.NET MVC中进行注册。 我正在使用SQL Server函数来检查是否存在具有相同用户名/电子邮件的用户。 我不是编程专业人士,因为可能会有一个到目前为止尚未找到的明显解决方案。

我试图通过控制器或SQL Server函数直接调用警报弹出消息,例如“已有用户使用相同的用户名/电子邮件。”。

我从SQL Server函数中返回了一些信息,并将其转换为控制器中的布尔值,并试图找到一种通过使用带模型的javascript来警告消息的方法,或者尝试使用View中的if语句通过控制器传递值的方式。

SQL Server功能代码:

CREATE FUNCTION [dbo].[userExists]
     (@email VARCHAR(1024), 
      @userName VARCHAR(1024))
RETURNS BIT
AS
BEGIN
    IF ((SELECT Email FROM Users WHERE Email = @email) IS NOT NULL)
    BEGIN
        RETURN 1
    END

    IF ((SELECT UserName FROM Users WHERE UserName = @userName) IS NOT NULL)
    BEGIN
        RETURN 1
    END

    RETURN 0
END

控制器动作代码:

SqlCommand eCmd = new SqlCommand("SELECT dbo.userExists(@email, @userName)", con);
eCmd.Parameters.AddWithValue("email", regModel.Email);
eCmd.Parameters.AddWithValue("userName", regModel.UserName);

bool exists = Convert.ToBoolean(eCmd.ExecuteScalar());

eCmd.Dispose();

if (exists != true)
{
    SqlCommand cmd = new SqlCommand("INSERT INTO Users (UserName, Email, Password) VALUES (@userName, @email, @password)", con);

    cmd.Parameters.AddWithValue("userName", regModel.UserName);
    cmd.Parameters.AddWithValue("email", regModel.Email);
    cmd.Parameters.AddWithValue("password", MD5Hash(regModel.Password));

    cmd.ExecuteNonQuery();
    cmd.Dispose();
}
else
{                                             
    return View();
}

到目前为止一切正常。 我要实现的是消息警报,指示用户是否已经存在。

我已经尝试研究如何使用“ return JavaScript(“ \\ code \\”);“ 但是,不想工作。

在您的controller方法中,您需要使用ExecuteNonQuery从sql语句中获取一些回报。 并且您必须将值存储在从函数获取的某些变量中。


if (exists != true)
{
    SqlCommand cmd = new SqlCommand("INSERT INTO Users (UserName, Email, Password) VALUES (@userName, @email, @password)", con);

    cmd.Parameters.AddWithValue("userName", regModel.UserName);
    cmd.Parameters.AddWithValue("email", regModel.Email);
    cmd.Parameters.AddWithValue("password", MD5Hash(regModel.Password));

    ViewBag.myval = cmd.ExecuteNonQuery();  // need to use ExecuteNonQuery 

    cmd.Dispose();
}
else
{                                             
    return View();
}

在视野中,获取您的价值

 @Model.myval 

之后,您可以使用javascript发布消息。 根据您的产品结构。

int result=0;
SqlCommand cmd = new SqlCommand("INSERT INTO Users (UserName, Email, Password) VALUES (@userName, @email, @password)", con);

cmd.Parameters.AddWithValue("userName", regModel.UserName);
cmd.Parameters.AddWithValue("email", regModel.Email);
cmd.Parameters.AddWithValue("password", MD5Hash(regModel.Password));

result=cmd.ExecuteNonQuery();
cmd.Dispose();

现在,根据您获得的值,您可以使用jQuery提醒消息。 您可以将此值作为Model或在viewBagviewBag 我会尝试用viewbag作为

ViewBag.resultOfQuery=result;
return View();

从jQuery的前端,您可以基于@ViewBag.resultOfQuery警报

<script>
 function YourFunctionName()
  {
    if('@ViewBag.resultOfQuery'=="1")
       alert("There is already a user with same Username/Email.");
    else
       alert("Inserted Successfully");
  }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM