页面加载时显示验证消息

时间:2018-03-21 15:03:00

标签: asp.net-mvc asp.net-mvc-4 data-annotations

我是ASP.NET MVC框架的新手,开始我的第一个项目。 我在模型类中使用了数据注释来设置验证消息。但验证消息显示在页面加载上。我想在点击按钮时显示验证消息。我怎么能这样做?

型号:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace final_1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class TBL_USER
    {
        public int USERID { get; set; }
        [Required(ErrorMessage = "Please enter user name.")]
        public string USERNAME { get; set; }
        public string NAME { get; set; }
        public int ROLE { get; set; }
        [Required(ErrorMessage = "Please enter password.")]
        public string PASSWORD { get; set; }
        public System.DateTime CREATED_DATE { get; set; }
        public int STATUS { get; set; }
    }
}

查看:

@using (Html.BeginForm("Authorize", "Login", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group has-feedback">
        @Html.TextBoxFor(m => m.USERNAME, new { @class = "form-control", @placeholder = "User name", @autofocus = "autofocus" })
        @Html.ValidationMessageFor(m => m.USERNAME, "", new { @class = "text-danger" })
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        @Html.PasswordFor(m => m.PASSWORD, new { @class = "form-control", @placeholder = "Password" })
        @Html.ValidationMessageFor(m => m.PASSWORD, "", new { @class = "text-danger" })
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
    </div>
    <div class="row">
        <div class="col-xs-8"></div>
        <div class="col-xs-4">
            <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
        </div>
    </div>
}

控制器:

public ActionResult Authorize(TBL_USER userModel)
{
    using (MIEntities db = new MIEntities())
    {
        var userDetails = db.TBL_USER.Where(a => a.USERNAME == userModel.USERNAME && a.PASSWORD == userModel.PASSWORD).FirstOrDefault();
        if (userDetails != null)
        {
            return RedirectToAction("Register", "Student");
        }
        else
        { return View(); }
    }
}

2 个答案:

答案 0 :(得分:0)

我建议使用ModelState进行检查,然后确保在返回View时返回ViewModel。

public ActionResult Authorize(TBL_USER userModel)
    {
        using (MIEntities db = new MIEntities())
        {
            var userDetails = db.TBL_USER.Where(a => a.USERNAME == userModel.USERNAME && a.PASSWORD == userModel.PASSWORD).FirstOrDefault();
            if (ModelState.IsValid)
            {
                return RedirectToAction("Register", "Student");
            }
            else
            { return View(userModel); }
        }
    }

答案 1 :(得分:0)

我为get请求和post请求创建了两种操作方法。

[HttpGet] 
        public ActionResult Authorize()
        {
            return View();
        }
        [HttpPost] 
        public ActionResult Authorize(final.Models.TBL_USER userModel)
        {
            using (LT_LGCDP_MISEntities1 db = new LT_LGCDP_MISEntities1())
            {
                var userDetails = db.TBL_USER.Where(a => a.USERNAME == userModel.USERNAME && a.PASSWORD == userModel.PASSWORD).FirstOrDefault();
                if(userDetails == null)
                {
                    return View(userModel);
                }
                    return RedirectToAction("Register", "Student");
            }
        }

有效!