我是mvc的新手。我已经完成了以下代码,但仍然无法验证客户端验证
在我的web.config中我有
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
在我的模特中我有
namespace MvcValidation.Models
{
public class EmailAddress
{
[Required]
[Display(Name="User ID")]
public string UserId { get; set; }
[Required]
[Display(Name = "User Name1")]
public string UserName { get; set; }
[Required]
[StringLength(20,MinimumLength=5)]
public string Password { get; set; }
[Required]
public string Email { get; set; }
[Required(ErrorMessage="Enter Website")]
public string Website { get; set; }
[Required]
public string Location { get; set; }
[Required]
public string State { get; set; }
[Required]
public string City { get; set; }
}
}
In Create.cshtml I have
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>Create</h2>
<table>
@using(Html.BeginForm())
{
<tr>
<td>@Html.DisplayNameFor(x=>x.UserId)</td>
<td>@Html.EditorFor(x=>x.UserId)</td>
<td>@Html.ValidationMessageFor(x=>x.UserId)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.UserName)</td>
<td>@Html.EditorFor(x=>x.UserName)</td>
<td>@Html.ValidationMessageFor(x=>x.UserName)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.Password)</td>
<td>@Html.EditorFor(x=>x.Password)</td>
<td>@Html.ValidationMessageFor(x=>x.Password)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.Email)</td>
<td>@Html.EditorFor(x=>x.Email)</td>
<td>@Html.ValidationMessageFor(x=>x.Email)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.Website)</td>
<td>@Html.EditorFor(x=>x.Website)</td>
<td>@Html.ValidationMessageFor(x=>x.Website)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.Location)</td>
<td>@Html.EditorFor(x=>x.Location)</td>
<td>@Html.ValidationMessageFor(x=>x.Location)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.State)</td>
<td>@Html.EditorFor(x=>x.State)</td>
<td>@Html.ValidationMessageFor(x=>x.State)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.City)</td>
<td>@Html.EditorFor(x=>x.City)</td>
<td>@Html.ValidationMessageFor(x=>x.City)</td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" value="Add" /></td>
</tr>
}
</table>
在HomeController我有
public ActionResult Create()
{
return View();
}
[ActionName("Create")]
[HttpPost]
public ActionResult Create_P(EmailAddress ed)
{
if(ModelState.IsValid)
{
RedirectToAction("Index");
}
return View(ed);
}
请帮助我,以便我可以使用客户端验证
答案 0 :(得分:0)
如果从空的MVC项目开始,则必须通过Nuget添加Scripts和Optimization框架。还要确保在Bundle config中正确配置了bundle并将其添加到Application_Start()方法中。在主布局中呈现脚本并添加对jquery脚本的引用。 Finnaly将验证脚本添加到局部视图中。