asp.net C#MVCを使用してサイトのログインを作成しています。だから私のビュー(UserLogin.cshtml)ファイルは次のようになります。プライマリキーを使用せずに情報を取得する方法asp.net C#?
@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-\._\+%][email protected](?:[\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を使用しています。しかし、ここで私はこの方法で特定のユーザーの詳細を取得しています。
文字列email = Request.Form ["email"]; string password = Request.Form ["password"];
UsersModel users = db.UsersModel.Find(email);
メールは私のプライマリキーではありません。それ以来、私はサイトにログインするとエラーが発生します。私はこれを解決する方法を知らない。
そして、あなたがしようとした場合:ユーザー= db.UserModel.FirstOrDefault(X => x.email ==メールを)? –