我正在布局一个比较两个密码字符串的视图.我的一个模型中的两个属性非常简单:
[Required]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
[StringLength(20, MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New Password")]
public string NewPassword { get; set; }
[Required]
[DataType(DataType.Password)]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
[StringLength(20, MinimumLength = 6)]
[Display(Name = "Confirm Password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是我的观看代码:
<table class="fieldset center" width="400">
<tbody>
<tr>
<th width="150">
@Html.LabelFor(m => m.NewPassword)
</th>
<td>
@Html.PasswordFor(m => m.NewPassword, …Run Code Online (Sandbox Code Playgroud) 在使用ajax提交表单之前,我需要检查数据库中是否已存在数据.最常见的情况是检查用户名和电子邮件的可用性.
它不工作,但我测试了ajax功能,而没有使用from控件,它工作正常.该项目是在MVC 3 VB中创建的.
使用Javascript:
$('#addSalesPeople').click(function () {
$.post("ajax_functions/checkDuplicate.cshtml",
{
extension: document.getElementById('Extension').value,
initials: document.getElementById('Initials').value
},
function (data, status) {
alert("Data: " + data + "\nStatus: " + status);
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
@Using Html.BeginForm("Create", "SalesPeople", FormMethod.Post)
@Html.ValidationSummary(True)
@<fieldset>
............................
..............................
@Html.TextBoxFor(Function(model) model.Extension, New With {.onkeyup = "javascript: charCounter(this,4);", .onblur = "javascript: zeroPad(this, 4)"})
@Html.TextBoxFor(Function(model) model.Initials)
<input id="addSalesPeople" class="btn span2" type="submit" value="Add" />
</fieldset>
Run Code Online (Sandbox Code Playgroud)
-谢谢
我在我的模型中使用远程属性来检查重复的页面标题如下
public class Page
{
[Remote("CheckDuplicate", "Page", ErrorMessage = "Title already taken")]
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,我将根据" 检查 "结果返回JsonResult数据,如下所示:
public JsonResult CheckDuplicate(string Title)
{
var result = db.Pages.Where(a => a.Title == Title).Count() == 0;
return Json(result, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
这在Create动作中工作正常,但问题是,它限制了我编辑现有页面,因为它正在检查相同的查询.
如何解决这个问题呢?请建议我