【发布时间】:2014-06-15 08:54:30
【问题描述】:
我使用带有服务层的 asp.net mvc 创建了一个表单来验证业务逻辑,而无需使用数据注释进行模型验证。
这是我在服务类中用于验证的代码
public class TutorService : ITutorService
{
private ModelStateDictionary modelstate;
private ITutorRepository repository;
public TutorService(ModelStateDictionary modelstate, ITutorRepository repository)
{
this.modelstate = modelstate;
this.repository = repository;
}
//Validation Logic
protected bool Validate(Tutor tutor)
{
if (tutor.FirstName==null)
modelstate.AddModelError("FirstName", "First Name is required.");
if (tutor.LastName == null)
modelstate.AddModelError("LastName", "Last Name is required.");
return modelstate.IsValid;
}
public bool CreateTutor(Tutor tutor)
{
if (Validate(tutor))
{
try
{
repository.CreateTutor(tutor);
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
public IEnumerable<Tutor> ListTutors()
{
return repository.ListTutors();
}
}
public interface ITutorService
{
bool CreateTutor(Tutor productToCreate);
IEnumerable<Tutor> ListTutors();
}
此方法用于验证模型。在这种机制中,客户端验证不起作用。单击提交按钮时,它会发回并显示错误消息。但是我想在没有后备支持的情况下显示错误消息。我已经加了
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
到 BundleConfig.cs 和
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
到 Web.config 文件。
这是导师创建视图:
@model MvcApplication7.Models.DB.Tutor
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Tutor</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address1)
@Html.ValidationMessageFor(model => model.Address1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address2)
@Html.ValidationMessageFor(model => model.Address2)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address3)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address3)
@Html.ValidationMessageFor(model => model.Address3)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Tel1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Tel1)
@Html.ValidationMessageFor(model => model.Tel1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Tel2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Tel2)
@Html.ValidationMessageFor(model => model.Tel2)
</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>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsConfirmed)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsConfirmed)
@Html.ValidationMessageFor(model => model.IsConfirmed)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
导师班:
public partial class Tutor
{
public Tutor()
{
this.Examinations = new HashSet<Examination>();
}
public decimal TutorID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Tel1 { get; set; }
public string Tel2 { get; set; }
public string EMail { get; set; }
public string Password { get; set; }
public Nullable<bool> IsConfirmed { get; set; }
public virtual ICollection<Examination> Examinations { get; set; }
}
【问题讨论】:
-
您的属性数据注释在哪里,您正在使用服务进行验证。
-
请出示
Tutor类 -
我不使用任何数据注释属性来验证内部模型。 Tutor 类是 EF 生成的普通类,只包含 FirstName、LastName 和其他一些属性,没有数据注释。
-
你能发表你的看法吗?
-
发布您的导师课程代码
标签: c# jquery asp.net-mvc client-side-validation service-layer