页面加载时显示的验证消息
Validation message displayed on page load
我是 ASP.NET MVC 框架的新手,开始我的第一个项目。
我在我的模型 class 中使用了数据注释来设置验证消息。但是验证消息显示在页面加载时。我希望在单击按钮时显示验证消息。我该怎么做?
型号:
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(); }
}
}
我建议使用 ModelState 进行检查,然后确保在 return 查看时 return 使用 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); }
}
}
我为 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");
}
}
成功了!
我是 ASP.NET MVC 框架的新手,开始我的第一个项目。 我在我的模型 class 中使用了数据注释来设置验证消息。但是验证消息显示在页面加载时。我希望在单击按钮时显示验证消息。我该怎么做?
型号:
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(); }
}
}
我建议使用 ModelState 进行检查,然后确保在 return 查看时 return 使用 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); }
}
}
我为 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");
}
}
成功了!