在没有回发的情况下在客户端显示模型错误

时间:2014-04-29 12:12:06

标签: c# jquery asp.net-mvc client-side-validation service-layer

我使用带有服务层的asp.net mvc创建了一个表单来验证业务逻辑,而无需使用Data Annotations进行模型验证。

这是我在验证服务类中的代码

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; }
}

3 个答案:

答案 0 :(得分:0)

也许帮助

  

Install-Package jQuery.Validation.Unobtrusive

答案 1 :(得分:0)

我认为您可以使用MetadataType数据注释为Tutor生成第二个类。

EF基本上生成Tutor作为部分类,您可以使用数据注释在同一名称空间中创建另一个类。

以下是

所需的内容
namespace MVCApplication
{
    [MetadataType(typeof(TutorMetaData))]//<- Just adding this class level attribute does the job for us.
    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; }
    }

    public class TutorMetaData
    {
        public Tutor()
        {
            this.Examinations = new HashSet<Examination>();
        }

        public decimal TutorID { get; set; }
        [Required] //<-- use as many annotations as you like
        public string FirstName { get; set; }
        [Required] //<-- use as many annotations as you like
        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; }
    }
}

希望它有所帮助。

答案 2 :(得分:0)

更改bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.unobtrusive*", "~/Scripts/jquery.validate*"));

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate*",
                "~/Scripts/jquery.unobtrusive*"));