gpt4 book ai didi

asp.net-mvc - ASP.NET MVC3 远程验证不输出错误消息

转载 作者:行者123 更新时间:2023-12-01 14:02:23 25 4
gpt4 key购买 nike

我有一个简单的模型,它是默认的 RegisterModel,在为 Create 创建一个基于该模型的 View 时,我最终得到了

public class RegisterModel
{
[Required]
[Remote("UserNameExists", "Account", "", ErrorMessage = "Username is already taken.")]
[Display(Name = "Username (spaces will be stripped, must be at least 6 characters long)")]
public string UserName { get; set; }

[Required]
[Editable(true)]
[Display(Name = "First and Last name")]
public string Name { get; set; }

[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "You need to enter a valid email")]
[Remote("EmailExists", "Account", "", ErrorMessage = "Email is already taken.")]
[Display(Name = "Email address")]
public string Email { get; set; }

//[Required]
//[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "Create a password")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Verify password")]
//[Compare("Password", ErrorMessage = "Password's do not match.")]
public string ConfirmPassword { get; set; }
}

在 View 中:

<h3>
Details</h3>
@using (Html.BeginForm("GenerateBetaLink", "Account", FormMethod.Post, new { @id = "beta-user" }))
{
@Html.ValidationSummary(true)
<div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
&nbsp;</p>
<p>
<input type="submit" value="Create Beta User" class="btn-submit" />
<span class="loading"></span>
</p>
</div>
}

我的验证 Controller

public class ValidationController : Controller
{
public JsonResult UserNameExists(string UserName)
{
OnlineServicesRepository db = new OnlineServicesRepository();

var user = db.FindUserByUsername(UserName.Trim());
return user == null ?
Json(true, JsonRequestBehavior.AllowGet) :
Json(string.Format("{0} is not available.", UserName),
JsonRequestBehavior.AllowGet);
}

public JsonResult EmailExists(string Email)
{
OnlineServicesRepository db = new OnlineServicesRepository();

var user = db.FindUserByEmail(Email.Trim());
return user != null ?
Json(true, JsonRequestBehavior.AllowGet) :
Json(string.Format("{0} is not available.", Email),
JsonRequestBehavior.AllowGet);
}
}

我的问题是远程验证确实触发了,但没有将任何内容写入错误消息,此外,jQuery方法 .valid() 不断告诉我 表单有效:

enter image description here
(来源:balexandre.com)

What am I missing here?

MSDN article显示相同的代码(在可下载文件中)

最佳答案

以下对我来说效果很好:

型号:

public class RegisterModel
{
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "You need to enter a valid email")]
[Remote("EmailExists", "Home", "")]
[Display(Name = "Email address")]
public string Email { get; set; }
}

Controller :

public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult Index(RegisterModel model)
{
return View(model);
}

public ActionResult EmailExists(string email)
{
if ((email ?? string.Empty).Contains("foo"))
{
return Json(email + " is not available", JsonRequestBehavior.AllowGet);
}

return Json(true, JsonRequestBehavior.AllowGet);
}
}

查看:

@model RegisterModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<input type="submit" value="OK" />
}

关于asp.net-mvc - ASP.NET MVC3 远程验证不输出错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5712924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com