[英]In asp.net mvc, how to add validations
I want to compare passwords and also a specified password length.我想比较密码和指定的密码长度。 There is some problem in the controller where I am updating it, it returns view only if the passwords are empty, then I applied condition to match them, but I want this to be done automatically like when I want to check the length I don't wnt to apply another condition in model.
我正在更新它的控制器中存在一些问题,仅当密码为空时它才返回视图,然后我应用条件来匹配它们,但我希望这能像我想检查长度时一样自动完成我不' t wnt 在模型中应用另一个条件。 Please help me what is the problem with my approach?
请帮助我我的方法有什么问题?
I have tried this code我试过这个代码
Controller:控制器:
[AllowAnonymous]
public ActionResult ResetPass()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult ResetPass(userPass model, string phoneNumber, String password, String repassword)
{
if (!String.IsNullOrEmpty(model.repassword) && !String.IsNullOrEmpty(model.password)&&model.password==model.repassword )
{
using (var db = new MongoContext())
{
db._database.GetCollection<userPass>("userPass");
var filter = Builders<BsonDocument>.Filter.Eq("_phoneNumber", phoneNumber);
var update = Builders<BsonDocument>.Update
.Set("password", password);
db._database.GetCollection<BsonDocument>("farmers").UpdateOne(filter, update, null);
return RedirectToAction("Login", new {Message = "password has been reset"});
}
}
return View(model);
}
Model:模型:
public class userPass
{
[BsonElement("password")]
[Required(ErrorMessage = "Password is required.")]
[StringLength(8, ErrorMessage = "Password length must be 8.")]
public string password { get; set; }
[BsonElement("repassword")
[Required(ErrorMessage = "Confirmation Password is required.")]
[Compare("password", ErrorMessage = "Password and Confirmation Password must match.")]
public string repassword { get; set; }
}
View:看法:
@using (Html.BeginForm("ResetPass", "Account", FormMethod.Post))
{
<table>
<tr>
<td>Password</td>
<td>@Html.PasswordFor(m => m.password)</td>
<td>@Html.ValidationMessageFor(m => m.password, "", new { @class = "error" })</td>
</tr>
<tr>
<td>Confirm Password</td>
<td>@Html.PasswordFor(m => m.repassword)</td>
<td>@Html.ValidationMessageFor(m => m.repassword, "", new { @class = "error" })</td>
</tr>
<tr>
<td></td>
<td><input type="submit" class="btn btn-primary block m-b" value="Submit" /></td>
<td></td>
</tr>
</table>
}
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
[HttpPost]
[AllowAnonymous]
public ActionResult ResetPass(userPass model)
{
if(ModelState.IsValid) {
}
return View(model)
}
Just a little bit of modification.只是一点点的修改。
For Minimum Length you can use range attribute -对于最小长度,您可以使用范围属性 -
[Range(8, 25, ErrorMessage = "Min Length should be 8")]
First paramter is minLength and second is maxLength第一个参数是 minLength,第二个是 maxLength
or或者
[MinLength(8, ErrorMessage = "Min Length should be 8")]
Edit compare attribute.编辑比较属性。
[Compare(CompareField = password, ErrorMessage = "Password and Confirmation Password must match.")]
here is detail example :这是详细示例:
public class userPass
{
[BsonElement("password")]
[Required(ErrorMessage = "Password is required.")]
[StringLength(8, ErrorMessage = "Password length must be 8.")]
public string password { get; set; }
[BsonElement("repassword")
[Required(ErrorMessage = "Confirmation Password is required.")]
[Compare(CompareField = password, ErrorMessage = "Password and Confirmation Password must match.")]
public string repassword { get; set; }
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.