I have created a form using asp.net mvc with service layer to validate business logic without model validation using Data Annotations.
This is my code in the service class for validation
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();
}
this method use for validate the model. In this mechanism client side validation does not work. When click the submit button it post back and display error messages. But I want to display error messages without post backing. I have already added
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
to BundleConfig.cs and
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
to Web.config file.
This Is Tutor Create View :
@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")
}
Tutor Class :
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; }
}