C# 在客户端显示模型错误而不回发

C# 在客户端显示模型错误而不回发,c#,jquery,asp.net-mvc,client-side-validation,service-layer,C#,Jquery,Asp.net Mvc,Client Side Validation,Service Layer,我使用asp.net mvc和服务层创建了一个表单,用于验证业务逻辑,而无需使用数据注释进行模型验证 这是我在服务类中进行验证的代码 public class TutorService : ITutorService { private ModelStateDictionary modelstate; private ITutorRepository repository; public TutorService(ModelStateDictionary

我使用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();
}
将ECONFIG.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")
}
@model mvcapapplication7.Models.DB.Tutor
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
家教
@LabelFor(model=>model.FirstName)
@EditorFor(model=>model.FirstName)
@Html.ValidationMessageFor(model=>model.FirstName)
@LabelFor(model=>model.LastName)
@EditorFor(model=>model.LastName)
@Html.ValidationMessageFor(model=>model.LastName)
@LabelFor(model=>model.Address1)
@EditorFor(model=>model.Address1)
@Html.ValidationMessageFor(model=>model.Address1)
@LabelFor(model=>model.Address2)
@EditorFor(model=>model.Address2)
@Html.ValidationMessageFor(model=>model.Address2)
@LabelFor(model=>model.Address3)
@EditorFor(model=>model.Address3)
@Html.ValidationMessageFor(model=>model.Address3)
@LabelFor(model=>model.Tel1)
@EditorFor(model=>model.Tel1)
@Html.ValidationMessageFor(model=>model.Tel1)
@LabelFor(model=>model.Tel2)
@EditorFor(model=>model.Tel2)
@Html.ValidationMessageFor(model=>model.Tel2)
@LabelFor(model=>model.EMail)
@EditorFor(model=>model.EMail)
@Html.ValidationMessageFor(model=>model.EMail)
@LabelFor(model=>model.Password)
@EditorFor(model=>model.Password)
@Html.ValidationMessageFor(model=>model.Password)
@LabelFor(model=>model.isconfirm)
@EditorFor(model=>model.isconfirm)
@Html.ValidationMessageFor(model=>model.isconfirm)

} @ActionLink(“返回列表”、“索引”) @节脚本{ @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; }
}
公共部分课程导师
{
公共导师()
{
this.executions=newhashset();
}
公共十进制教程{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串地址1{get;set;}
公共字符串地址2{get;set;}
公共字符串地址3{get;set;}
公共字符串Tel1{get;set;}
公共字符串Tel2{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
公共可为Null的已确认{get;set;}
公共虚拟ICollection考试{get;set;}
}
也许有帮助

安装程序包jQuery.Validation.Unobtrusive


我认为可以使用
MetadataType
data注释为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; }
    }
}
命名空间应用程序
{
[MetadataType(typeof(TutorMetaData))]/change
bundles.Add(newscriptbundle(“~/bundles/jqueryval”)。包括(
“~/Scripts/jquery.unobtrusive*”,
“~/Scripts/jquery.validate*”);


您的属性数据注释在哪里,您正在使用服务进行验证。请显示
Tutor
classI不使用任何数据注释属性在模型内部进行验证。Tutor类是EF生成的普通类,它只包含FirstName、LastName和一些没有数据注释的其他属性。您可以发布视图吗?发布tutor类代码添加了Attribute tutor.FirstName[Remote(“Action”、“Controller”)]?就在问题答案的下方在某些情况下,一个属性使用多个视图并向同一注释显示多条错误消息(例如:创建视图显示一条错误消息,编辑视图向同一注释显示另一条错误消息)。我认为这种方法不是最好的。而且[MetadataType(typeof(TutorMetaData))]代码在edmx更改时也会刷新。
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; }
    }
}
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate*",
                "~/Scripts/jquery.unobtrusive*"));