【问题标题】:Show model errors in client side without postback在客户端显示模型错误而不回发
【发布时间】: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


【解决方案1】:

也许有帮助

安装包 jQuery.Validation.Unobtrusive

【讨论】:

  • 添加属性tutor.FirstName [Remote("Action", "Controller")] ?
  • 就在您问题的答案下方
【解决方案2】:

我觉得你可以使用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; }
    }
}

希望对你有帮助。

【讨论】:

  • 在某些情况下,一个属性使用多个视图,并且对同一个注释有多个错误消息(例如:创建视图显示一个错误消息,编辑视图显示另一个错误消息到同一个注释)。我认为这种方法不是最好的。并且 [MetadataType(typeof(TutorMetaData))] 代码也会在 edmx 更改时刷新。
【解决方案3】:

更改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*"));

【讨论】:

    猜你喜欢
    • 2015-10-28
    • 2012-08-17
    • 2019-10-05
    • 2016-07-11
    • 2016-03-31
    • 2021-12-24
    • 2012-10-29
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多