c# - 使用存储库模式 LINQ 查询的登录页面。出现错误
问题描述
我正在使用存储库模式。当我登录时,我收到错误:
“你调用的对象是空的。”
一切都很完美,但我不知道为什么会出现这个错误,谁能告诉我哪里错了。LoginViewModel 和用户模型是相同的,我使用的是数据库优先方法。
登录页面
public ActionResult Login(LoginViewModel loginModel )
{
try
{
if (!string.IsNullOrEmpty(loginModel.Username) && !string.IsNullOrEmpty(loginModel.Password))
{
var Username = loginModel.Username;
var Password = loginModel.Password;
var result = _IUserRepository.ValidUser(Username, Password);
if (result.id==0 || result.id < 0)
{
ViewBag.errormessage = "Enter Invalid Username and Password";
}
else
{
return RedirectToAction("Index");
}
}
}
catch (Exception)
{
throw;
}
return View();
}
用户存储库
public class UserRepository : IUserRepository
{
private dbContext _context;
public UserRepository(dbContext context)
{
this._context = context;
}
public bool UpdatePassword(user user)
{
throw new NotImplementedException();
}
public user ValidUser(string username, string passWord)
{
try
{
var validate = (from user in _context.users
where user.Username == username && user.Password == passWord
select user).SingleOrDefault();
return validate;
}
catch (Exception ex)
{
throw ex;
}
}
}
IUserRepository
public interface IUserRepository
{
user ValidUser(string username, string password);
bool UpdatePassword(user user);
}
登录视图模型
public class LoginViewModel
{
public int id { get; set; }
[Required(ErrorMessage ="Username is Required")]
public string Username { get; set; }
[Required(ErrorMessage = "Password is Required")]
public string Password { get; set; }
}
登录.chtml
@model RepositoryPattren.Models.LoginViewModel
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LoginViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
解决方案
尝试在您的代码中检查这些情况: 1 - 检查以确保您收到的登录模型不为空 2- 您是否注册了 IUserRepository 依赖项?如果您没有注册,则不会注入实例 3-您正在使用 SingleOrDefault 那么如果条件不匹配,它将返回 null 作为默认值,并且您没有检查 null 所以请执行以下代码:
if (result!=null && result.id==0 || result.id < 0)
{
ViewBag.errormessage = "Enter Invalid Username and Password";
}
else
{
return RedirectToAction("Index");
}
推荐阅读
- equation - Modelica中的Weissinger方程解
- python - Pandas value_counts() 替代一系列值?
- awk - 如何在grep中显示匹配之前或之后的所有行?
- python - 如何用单独的置信区间分割小提琴图
- python - 关于 python 中的 raw_input.split() 函数
- reactjs - 如何使用 react-redux 显示所有用户的列表?
- python - 在 lambda 中运行 os.system 模块时遇到“sh: php: command not found”错误
- python - 检测多维数据中的空隙
- java - 请求用户权限对话框语言基于 Android System Locale 而不是 APP Locale
- swift - 类型没有成员下标,在带有 CoreData 的 SwiftUI 中