我正在创建一个表单,以便站点的成员可以更改其帐户的密码,并且我想显示一条消息,解释用户在填写字段时所犯的错误(例如密码太短,至少需要一个非字母数字字符等)。我希望将这些消息显示在同一页面中,字段名称旁边。下面是我的代码:
@helper RenderForm()
{
<form method="post">
<p>Change your password below</p>
<div><label for="currentPassword">Current Password</label>
<input type="password" id="currentPassword" name="currentPassword"/></div>
<div><label for="newPassword">New Password:</label>
<input type="password" id="newPassword" name="newPassword"/></div>
<div><label for="confirmPassword">Confirm New Password</label>
<input type="password" id="confirmPassword" name="confirmPassword"/></div>
<div><input type="submit" id="submit" name="submit" value="submit"/></div>
</form>
}
@helper Message(string message)
{
<p>@message</p>
}
<style type="text/css">
p,label {color:black;}
</style>
@{
if(!IsPost) {
@RenderForm();
}
else {
var account = Membership.GetUser();
var currentPassword = HttpContext.Current.Request["currentPassword"];
if(Membership.ValidateUser(account.UserName, currentPassword)){
var newPassword = HttpContext.Current.Request["newPassword"];
var confirmPassword = HttpContext.Current.Request["currentPassword"];
if(check(newPassword, confirmPassword)){
account.ChangePassword(account.ResetPassword(), newPassword);
}
}
else {
@Message("The password provided didn't match with the database.");
}
}
}
@functions{
List<string> check(string newPassword, string confirmPassword)
{
//just a place holder
return false;
}
}我尝试在发现错误时添加一个要填充的列表,当表单重新加载时会显示消息,但是RenderForm()函数找不到对该列表的任何引用。如何显示这些消息?
发布于 2013-08-08 16:20:51
您应该使用Web Pages框架附带的内置验证。我有一篇文章解释了如何使用它:http://www.mikesdotnetting.com/Article/191/Validation-In-Razor-Web-Pages-2
https://stackoverflow.com/questions/18107831
复制相似问题