我的实体框架工作模型状态验证不起作用我试了很多东西,但仍然无法正常工作?
代码=>
public class HomeController : Controller
{
private TsmsDBContext db = new TsmsDBContext();
[HttpGet]
public ActionResult Index()
{
db.Users.ToList();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "UserId,password")] User user)
{
Response.Write((user.UserId + user.password).ToString());
if (ModelState.IsValid)
{
Response.Write((user.UserId+user.password).ToString());
}
return View(user);
}
}
http get index方法是用于显示登录页面?
并且post仅用于获取用户ID和密码并显示它。但是用户ID和密码显示在if(ModelState.IsValid)
之前?但在它之后它没有显示。我的一切都很好。但是不知道它为什么不起作用?
我的模型类USER =>
public class User
{
[Key]
[Required(AllowEmptyStrings = false, ErrorMessage = "User Id Cannot Be Empty")]
[MaxLength(12, ErrorMessage = "You Have Exceed The Max length Of User ID which is [12] character")]
[RegularExpression("[1-3]{2}-[0-9]{5}-[123]{1}|[1-3]{2}-[0-9]{7}-[123]{1}", ErrorMessage = "Invalid Id,It should [xx]-[xxxxx]-[x] or [xx]-[xxxxxxx]-[x]")]
[Display(Name = "User ID")]
public string UserId { get; set; }
/// <summary>
/// ////////
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Password Cannot Be Empty")]
[MaxLength(20, ErrorMessage = "Password Max Length Is 20 Character")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string password { get; set; }
/// <summary>
/// ////////
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "User Level Cannot Be Empty")]
[MaxLength(100, ErrorMessage = "The Max Length For User Level Is 100 Character")]
[Display(Name = "User Level")]
public string level { get; set; }
//relationship with other table----
//public List<UserDetail> UserDetails { get; set; }
}
和我的索引视图=&gt;
<form id="login-form" method="post" role="form" style="display: block;">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="form-group">
@Html.TextBoxFor(model => model.UserId, new { @class = "form-control" , @placeholder = Html.DisplayNameFor(model => model.UserId)})
@Html.ValidationMessageFor(model => model.UserId)
</div>
<div class="form-group">
@Html.TextBoxFor(model => model.password, new { @class = "form-control" , @placeholder = Html.DisplayNameFor(model => model.password) })
@Html.ValidationMessageFor(model => model.password)
</div>
<div class="form-group text-center">
<input type="checkbox" tabindex="3" class="" name="remember" id="remember">
<label for="remember"> Remember Me</label>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-12">
<div class="text-center">
<a href="" tabindex="5" class="forgot-password">Forgot Password?</a>
</div>
</div>
</div>
</div>
</form>
我可能因为某些原因而无法工作。所以我试图改变=&gt;
@using (Html.BeginForm()) {
}
处于标签状态但没有工作。我也删除了antiforgari令牌。但仍然无法正常工作? 任何解决方案?
答案 0 :(得分:1)
ModelState
无效,因为level
模型的user
属性为null
将其添加到表单中的HTML:
@Html.HiddenFor(model => model.level)
答案 1 :(得分:0)
您的模型状态中有1个错误。因为您在user
的模型类level
中创建了必需属性。但在您看来,您没有任何文本框或地点可以进入关卡。因此,在这种情况下,您的level
属性为空。正如Sajal所说,你应该将它作为隐藏财产。
或者您可以清除级别属性的错误。