我正在使用asp.net C#MVC为网站创建一个Login。所以这是我的视图(UserLogin.cshtml)文件。
predictions = sess.run(y, feed_dict={x: data})
这是我的模型(UsersModel.cs)看起来像。
@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>
我的Controller(UserLogin.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; }
}
}
在我的控制器中有一个我无法解决的错误。这意味着在我的模型类中,我使用了UserModelID作为主键。但在这里,我正在以这种方式检索特定用户的详细信息。
string email = Request.Form [“email”]; string password = Request.Form [“password”];
UsersModel users = db.UsersModel.Find(email);
但是电子邮件不是我的主要关键。因为它会在登录网站时给我一个错误。我不知道如何解决这个问题。
答案 0 :(得分:0)
UsersModel users = db.UsersModel.FirstOrDefault(m =&gt; m.email == email)
答案 1 :(得分:0)
db.Find(key)将只使用主键来查找实体,您可以尝试使用db.UsersModel.FirstOrDefault。这可能会返回null,请确保在尝试使用返回的实体之前检查null,例如如下面的代码所示。
$links=array(
7 => 'http://domain7.com',
4 => 'http://domain4.com',
3 => 'http://domain3.com'
);
foreach( $links as $key => $url ) {
echo $key,' ',$url,'<br />';
}
&#13;