ПользователиModel users = db.UsersModel.FirstOrDefault (m => m.email == email)
Я создаю Login для сайта, используя asp.net C # MVC. Итак, вот мой файл (UserLogin.cshtml) выглядит так.
@model FoodieWeb.Models.UsersModel
@{
ViewBag.Title = "UserLogin";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@*<h2>UserLogin</h2>*@
<div class="container">
<div class="col-md-8">
<section id="loginForm">
@using (Html.BeginForm("UserLogin", "UserLogin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Use a local account to log in.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label for="email" class="col-md-2 control-label">Email</label>
<div class="col-md-10">
@*@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })*@
<input type="text" name="email" class="form-control" id="email" required="" data-validation-required-message="Please enter your Username." />
@*@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-2 control-label">Password</label>
<div class="col-md-10">
<input type="password" name="password" class="form-control" id="password" required="" data-validation-required-message="Please enter your Username." />
@*@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@*@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)*@
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-primary" />
</div>
</div>
<p>
@Html.ActionLink("Register as a new user", "UserRegister", "UserRegister")
</p>
@* Enable this once you have account confirmation enabled for password reset functionality
<p>
@Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>*@
}
</section>
</div>
<div class="col-md-4">
@*<section id="socialLoginForm">
@Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
</section>*@
</div>
</div>
<p>
@*@Html.ActionLink("Edit", "Edit", new { id = Model.UsersModelID }) |
@Html.ActionLink("Back to List", "Index")*@
</p>
И вот моя модель (UsersModel.cs) выглядит.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace FoodieWeb.Models
{
[Table("User")]
public class UsersModel
{
public int UsersModelID { get; set; }
[Required(ErrorMessage = "Email is required")]
[RegularExpression(@"^([w-._+%]+@(?:[w-]+.)+[w]{2,6})$", ErrorMessage = "Please enter a valid email")]
public string email { get; set; }
[Required(ErrorMessage = "First name is required")]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$", ErrorMessage = "Please enter lowercase & uppercase letters only")]
public string fname { get; set; }
[Required(ErrorMessage = "Last name is required")]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$", ErrorMessage = "Please enter lowercase & uppercase letters only")]
public string lname { get; set; }
[Required(ErrorMessage = "Phone number is required")]
[RegularExpression(@"^([(]{1}[0-9]{3}[)]{1}[.| |-]{0,1}|^[0-9]{3}[.|-| ]?)?[0-9]{3}(.|-| )?[0-9]{4}$", ErrorMessage = "Please enter a valid phone number")]
public string phone { get; set; }
[Required(ErrorMessage = "Address is required")]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$", ErrorMessage = "Please enter lowercase & uppercase letters only")]
public string address { get; set; }
[Required(ErrorMessage = "City is required")]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$", ErrorMessage = "Please enter lowercase & uppercase letters only")]
public string city { get; set; }
[Required(ErrorMessage = "PostalCode is required")]
[RegularExpression(@"d{5}(-d{4})?$", ErrorMessage = "Please enter a valid phone number")]
public Nullable<decimal> postcode { get; set; }
[Required(ErrorMessage = "District is required")]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$", ErrorMessage = "Please enter lowercase & uppercase letters only")]
public string district { get; set; }
[Required(ErrorMessage = "User Type is required")]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$", ErrorMessage = "Please enter lowercase & uppercase letters only")]
public string user_type { get; set; }
public string status { get; set; }
[Required(ErrorMessage = "Date is required")]
public System.DateTime created_date { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
public string password { get; set; }
[Required(ErrorMessage = "Confirm password is required")]
[Compare("password", ErrorMessage = "Confirm password does not match")]
[DataType(DataType.Password)]
public string confirm_password { get; set; }
[Required]
public Nullable<bool> agree { get; set; }
}
}
А также мой контроллер (UserLogin.cs) выглядит так.
using FoodieWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FoodieWeb.Controllers
{
public class UserLoginController : Controller
{
private UsersModelContext db = new UsersModelContext();
// GET: UserLogin
public ActionResult UserLogin()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserLogin(UsersModel usersModel)
{
string email = Request.Form["email"];
string password = Request.Form["password"];
UsersModel users = db.UsersModel.Find(email);
if (ModelState.IsValid)
{
if ((email.Equals(usersModel.email)) && (password.Equals(usersModel.password)))
{
Session["Email"] = email;
Session["LoggedIn"] = "yes";
return RedirectToAction("Index", "Home");
}
else
{
return View("~/Views/UserLogin/UserLogin.cshtml");
}
}
return View(usersModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserLogOff()
{
//AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
System.Web.HttpContext.Current.Session["LoggedIn"] = null;
return RedirectToAction("Index", "Home");
}
public object[] UsersModelID { get; set; }
}
}
В моем контроллере есть ошибка, которую я не могу решить. Это означает, что в моем классе модели я использовал UserModelID в качестве первичного ключа. Но здесь я получаю информацию о конкретном пользователе таким образом.
string email = Request.Form ["email"]; string password = Request.Form ["password"];
ПользователиModel users = db.UsersModel.Find (электронная почта);
Но электронная почта не является моим основным ключом. С тех пор это даст мне ошибку при входе на сайт. Я не знаю, как это исправить.
c#,
db.Find (key) использует только первичный ключ для поиска объекта, вы можете попробовать использовать db.UsersModel.FirstOrDefault. Это может вернуть значение null, убедитесь, что вы проверяете значение null, прежде чем пытаться использовать возвращаемый объект, например, как в приведенном ниже коде.
using FoodieWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FoodieWeb.Controllers
{
public class UserLoginController : Controller
{
private UsersModelContext db = new UsersModelContext();
// GET: UserLogin
public ActionResult UserLogin()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserLogin(UsersModel usersModel)
{
string email = Request.Form["email"];
string password = Request.Form["password"];
UsersModel users = db.UsersModel.FirstOrDefault(x=>x.email==email);
if (ModelState.IsValid&&users!=null)
{
if ((email.Equals(users.email)) && (password.Equals(users.password)))
{
Session["Email"] = email;
Session["LoggedIn"] = "yes";
return RedirectToAction("Index", "Home");
}
else
{
return View("~/Views/UserLogin/UserLogin.cshtml");
}
}
return View(usersModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserLogOff()
{
//AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
System.Web.HttpContext.Current.Session["LoggedIn"] = null;
return RedirectToAction("Index", "Home");
}
public object[] UsersModelID { get; set; }
}
}