Model层
[DisplayName("用户邮箱")]
[Required(AllowEmptyStrings = false, ErrorMessage = "邮箱不能为空")]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "{0}的格式不正确")]
[DataType(DataType.EmailAddress)]
[Remote("CheckEmail","User",ErrorMessage="此邮箱已经被注册")]
public string Email
{
set { _email = value; }
get { return _email; }
}
/// <summary>
/// 确认邮箱
/// </summary>
[Display(Name="确认邮箱")]
[Compare("Email", ErrorMessage = "邮箱必须一致")]
public virtual string EmailConfirm
{
get;
set;
}
view1(注册页)
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="indent">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
view2(找回密码)
<div class="editor-label"> @Html.LabelFor(model => model.Email) </div>
<div class="indent"> @Html.EditorFor(model => model.Email)
</div>
<div class="editor-label"> @Html.LabelFor(model => model.EmailConfirm) </div>
<div class="indent"> @Html.EditorFor(model => model.EmailConfirm) @Html.ValidationMessageFor(model => model.EmailConfirm) </div>
<p>
<input name="submit" type="submit" value="下一步" />
</p>
当我点击提交的时候,view2不能提交,我发现这个和Email验证有关,因为这个视图的Emailconfirm要用到验证,而Email字段不需要,老是不能提交,怎么解决,请大侠帮忙,提前感谢!
1、自定义验证
2、创建两套 Model 模板
这是最佳答案吗,我本来想这么弄的,但是觉得应该有更好的办法,你能不能再想想?当然多谢你们的回答。
@Html.ValidationMessageFor(model => model.EmailConfirm) 删掉
已经删掉了,不行
两个ViewModel不就可以了吗
一般都是使用两个ViewModel的