Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 未显示验证错误(MVC4和EF)_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Authentication - Fatal编程技术网

Asp.net mvc 未显示验证错误(MVC4和EF)

Asp.net mvc 未显示验证错误(MVC4和EF),asp.net-mvc,entity-framework,asp.net-mvc-4,authentication,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Authentication,由于用户名或密码不正确,未显示无效登录错误消息。我有一个名为User的模型和一个带有动作方法Validate的控制器,该方法验证用户名和密码。验证成功后,我重定向到createaction方法,否则我会添加一个模型错误,并希望在登录屏幕上显示一条无效的用户名或密码消息 Model: public class User { public int ID { get; set; } [Required] [Display(Name="User Name")] publ

由于用户名或密码不正确,未显示无效登录错误消息。我有一个名为User的模型和一个带有动作方法Validate的控制器,该方法验证用户名和密码。验证成功后,我重定向到createaction方法,否则我会添加一个模型错误,并希望在登录屏幕上显示一条无效的用户名或密码消息

Model:

public class User
{
    public int ID { get; set; }
    [Required]
    [Display(Name="User Name")]
    public string UserName { get; set; }
    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
    [Required]
    [Display(Name="First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name="Last Name")]
    public string LastName { get; set; }
    [Required]
    [DataType(DataType.PhoneNumber)]
    [MinLength(10)]
    [MaxLength(10)]
    [Display(Name="Mobile No")]
    public string PhoneNum { get; set; }
}

    Controller:

    [HttpGet]
    public ActionResult Validate()
    {

        return View();

    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Validate(User user)
    {


        var u1 = db.Users.Where(p => p.UserName == user.UserName && p.Password == user.Password).FirstOrDefault();
        if (u1 != null)
        {
            return RedirectToAction("Create");
        }
        else
        {

            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
        return RedirectToAction("Validate");


    }

    View:

    @model HindiMovie.Models.User

    @{ViewBag.Title = "Login";}

    <h2>Login</h2>

    @using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false,"The user name or password provided is incorrect.")

    <fieldset>
    <legend>User</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.UserName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.UserName)
        @Html.ValidationMessageFor(model => model.UserName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
        @Html.PasswordFor(model => model.Password)
        @Html.ValidationMessageFor(model => model.Password)
    </div>





    <p>
        <input type="submit" value="Validate" />
    </p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
重定向将重置ModelState。您可能希望重新显示视图:

public ActionResult Validate(User user)
{
    var u1 = db.Users.Where(p => p.UserName == user.UserName && p.Password == user.Password).FirstOrDefault();
    if (u1 != null)
    {
        return RedirectToAction("Create");
    }

    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View();
}