我是ASP.net MVC的新手,我正在尝试构建一个非常基本的登录表单,一切正常,我已经添加了登录方法的存储过程和“添加函数导入”,问题是它总是在所有情况下返回一个值“-1”。我们来看看......
1- MovableAssetsDB.context.cs
public virtual int LoginToMovable(string userName, string password)
{
var userNameParameter = userName != null ?
new ObjectParameter("UserName", userName) :
new ObjectParameter("UserName", typeof(string));
var passwordParameter = password != null ?
new ObjectParameter("Password", password) :
new ObjectParameter("Password", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("LoginToMovable", userNameParameter, passwordParameter);
}
2-我创建了一个名为“Data”的类来处理该方法并调用存储过程
public int Login(string username, string password)
{
using (var DBcontext = new MovableAssetsEntities())
{
int userId = DBcontext.LoginToMovable(username, password);
return userId;
}
}
3 - 在“主页”视图中,这是处理登录过程的表单和剃刀代码的代码
@{
Layout = null;
}
@{
ViewBag.Title = "Login";
}
@{
if (IsPost)
{
string username = Request.Form["username"];
string password = Request.Form["password"];
int userId = new MovableAssets.Classes.Data().Login(username, password);
if (userId > 0)
{
<script>alert("yes");</script>
}
else
{
<script>alert("No");</script>
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Movable Assets</title>
<link rel="stylesheet" href="@Url.Content("~/Content/Custom/css/style.css")">
</head>
<body>
<div class="container">
<p id="home_title">Movable Assets</p>
<section id="content">
<form action="#" method="post">
<h1>Login</h1>
<div>
<input type="text" placeholder="Username" required="" id="username" name="username" />
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password" />
</div>
<div>
<input type="submit" value="Log in" />
</div>
</form><!-- form -->
</section><!-- content -->
</div><!-- container -->
</body>
</html>
4 - 存储过程
ALTER PROCEDURE [dbo].[LoginToMovable]
@UserName nvarchar(50),
@Password nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT ID FROM Utilities.dbo.Users WHERE Utilities.dbo.Users.LogIn = @UserName AND Utilities.dbo.Users.Password = @Password
AND AppCode = '9'
END
每当我写下任何登录凭据时,我都会将userId
的值视为-1
任何建议?!
答案 0 :(得分:4)
存储过程的EF模型返回SELECT
,因为从EF调用存储过程的默认行为是返回由其最后一个语句修改的行数。由于最后一个语句是-1
,因此SELECT
无法修改行,因此您获得DBcontext.LoginToMovable()
:
https://stackoverflow.com/a/2203421/1945651
为了通过您的程序获取选择的值,您需要修改模型:
{{1}}现在应该返回一组值。调用它之后,从集合中选择第一个元素以获得所需的值。