我有注册模型
我的模特课
public class RegisterViewModel
{
[Required]
[StringLength(100, ErrorMessage = "Please enter a minimum of {2} characters", MinimumLength = 6)]
[DisplayName("University ID")]
[Remote("doesusernameExist", "HEC",null, ErrorMessage = "usr name is allready exist", HttpMethod = "POST")]
public string usrname { get; set; } }
我的json控制器类
[HttpPost]
public JsonResult doesusernameExist(string usrname)
{
var institute = db.Institutes.Find(HEI_ID);
return Json(institute == null);
}
用于创建新用户并使用上述模型编辑用户I。所以没有创建另一个模型,我想在编辑方法中禁用dousernameExist调用方法
答案 0 :(得分:1)
首先在编辑视图中禁用username
的客户端验证:
@Html.TextBoxFor(m => m.username, new { @data_val = "false" })
编辑帖子操作中的第二个从username
删除ModelState
的验证结果:
public ActionResult EditUser([Bind(Exclude = "usrname")]RegisterViewModel model)
{
ModelState.Remove("username");
if (ModelState.IsValid)
{
.
.
.
答案 1 :(得分:1)
首先在“查看方式”中添加“隐藏”字段:
@Html.hidden("PreviousUsername", Model.usrname)
在您的.cs类中添加以下内容:
[Remote("doesusernameExist", "HEC", ErrorMessage = "usr name is allready exist", AdditionalFields = "PreviousUsername")]
public string usrname { get; set; }
您的控制器操作方法应类似于:
public JsonResult doesusernameExist(string usrname, string PreviousUsername)
{
if(usrname==PreviousUsername)
{
return true;
}
var institute = db.Institutes.Find(HEI_ID);
return Json(false, JsonAlloBehaviour.AllowGet);
}