这是我的 asp.net mvc 2 项目的模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web.Security;
using System.Globalization;
using System.Web.Profile;
namespace EMS.Models
{
public class UserModels
{
[Required(ErrorMessage = "*")]
public string userName { get; set; }
[Required(ErrorMessage = "*")]
public string passWord { get; set; }
}
}
这是我的观点:
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) { %>
<table>
<tr>
<td><label for="userlogin">User Login:</label></td>
<td>
<%: Html.TextBoxFor(m => m.userName, new { id = "userName"})%>
<%: Html.ValidationMessageFor(m => m.name)%>
</td>
<td><label for="password">Password:</label></td>
<td>
<%: Html.PasswordFor(m => m.passWord, new { id = "password"})%>
<%: Html.ValidationMessageFor(m => m.passWord)%>
</td>
</tr>
<tr>
<td colspan="4">
<input type="submit" name="enter_infor" id="enter_infor" value="Enter Information"/>
</td>
</tr>
</table>
这是我的控制器:
[HttpPost]
public ActionResult UserMaintenance(FormCollection frm)
{
UserModels candidate = new UserModels
{
userName = frm["userName"].ToString(),
passWord = frm["passWord"].ToString()
};
DBSEntities context = new DBSEntities();
UserName user = new UserName();
context.AddToUserNames(user);
context.SaveChanges();
return View();
}
问题:我想验证用户是否输入了用户名和密码文本框。但是我上面的所有代码,用户仍然可以在没有任何验证消息的情况下提交它。我将脚本包含在 Site.Master 中。谁能告诉我,我做错了什么?
谢谢。