使用partial class和Validate

时间:2017-01-18 15:46:02

标签: asp.net-mvc validation partial-views

我有一个包含表单和部分视图的页面(也包含一个表单)。 两个模型都具有1个(或更多)具有相同名称的属性。当我验证第一个表单时,值和验证消息在第二个表单上是重复的。

我用虚拟实体创建了一个小样本。

person.cs

public partial class Person : IValidatableObject
{
    [Required(ErrorMessage = "name required")]
    public string Name { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {

        var results = new List<ValidationResult>();
        if (Name == "admin") //just example
        {
            results.Add(new ValidationResult("You cant be admin.", new[] { "Title", "Name" }));
        }
        return results;
    }
}   

人/ Index.cshtml

@model Person
@{
    ViewBag.Title = "Person";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("Index", "Person", FormMethod.Post, new { id = "CreatePersonForm" }))
{
    @Html.AntiForgeryToken()
    @Html.DisplayNameFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
    <input type="submit" value="Save" class="btn btn-default" />
}

@Html.Partial("~/Views/Dog/Index.cshtml", new Dog())

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

PersonController.cs

 public class PersonController : Controller
    {
        // GET: Person
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index([Bind(Include = "Name")] Person person)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }

            return View(person);
        }

    }

我的部分观点几乎相同。

Dog.cs

public partial class Dog : IValidatableObject
{
    [Required(ErrorMessage = "name required")]
    public string Name { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {

        var results = new List<ValidationResult>();
        if (Name == "admin") //just example
        {
            results.Add(new ValidationResult("You cant be admin.", new[] { "Title", "Name" }));
        }
        return results;
    }
}   

狗/ Index.cshtml

@model Dog
@{
    ViewBag.Title = "Dog Page";
}

@using (Html.BeginForm("Index", "Dog", FormMethod.Post, new { id = "CreateDogForm" }))
{
    @Html.AntiForgeryToken()
    @Html.DisplayNameFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
    <input type="submit" value="Save" class="btn btn-default" />
}


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

DogController.cs

 public class DogController : Controller
{
    // GET: Dog
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index([Bind(Include = "Name")] Dog dog)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("Index");
        }

        return View(dog);
    }

}

如果您开始/人/索引,如果您在第一个文本框(人员表格)中编写管理员,则在发布(保存)之后,第二个表单(狗表单)具有与第一个表单相同的文本和验证。

first view before action

write information and save

result with duplication information

1 个答案:

答案 0 :(得分:0)

默认情况下,@Html.EditorFor使用属性名称作为生成的id的{​​{1}}和name,验证使用这些值来设置错误消息!您可以传递一个值来覆盖部分视图中的默认行为,如下所示:

HTML