01/10/2018, 10:08

Tại sao custom validation trong Partial View thì error message không hiển thị?

Mình có file ProductModel.cs

namespace TestCustomValidation.Models
{
    public class ProductModel
    {
        [Required(ErrorMessage = "It's for test for ProductName")]
        public string ProductName { get; set; }

        [CustomRequired(ErrorMessage = "It's for test for ProductDescription")]
        public string ProductDescription { get; set; }
    }
}

Mình có file index.cshtml

...
@{Html.RenderAction("Form");}
...

file _FormPartial.cshtml

@model TestCustomValidation.Models.ProductModel
@using (Html.BeginForm("FormSubmit", "Home", new { @id = "form", @name = "form" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th colspan="2" align="center">Person Details</th>
        </tr>
        <tr>
            <td>Name: </td>
            <td>
                @Html.TextBoxFor(m => m.ProductName)
            </td>
            <td>
                @Html.ValidationMessageFor(m => m.ProductName)
            </td>
            
        </tr>
        <tr>
            <td>Description: </td>
            <td>
                @Html.TextBoxFor(m => m.ProductDescription)
            </td>
            <td>
                @Html.ValidationMessageFor(m => m.ProductDescription)
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit" /></td>
        </tr>
    </table>
}

file HomeController.cs

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public PartialViewResult Form()
        {
            ProductModel model = new ProductModel();
            return PartialView("_FormPartial", model);
        }

        [HttpPost]
        public ActionResult FormSubmit(ProductModel model)
        {
            if (ModelState.IsValid)
            {
                string name = model.ProductName;
                return RedirectToAction("About");
            }

            return PartialView("_FormPartial", model);
        }
}

file CustomRequiredAttribute.cs

namespace TestCustomValidation.CustomValidation
{
    public class CustomRequiredAttribute : RequiredAttribute
    {
    }
}

Khi mình nhấn nút Submit trên trang index thì kết quả như sau:

Cho mình hỏi tại sao mình chỉ custom required attribute thôi mà error message cho ProductDescription không hiện ra ?
Mình không hiểu tại sao luôn.

Tân Đặng viết 12:23 ngày 01/10/2018

Sax, mình quên đăng ký nó trong file Global.asax.cs

protected void Application_Start()
        {
            // Add Custom Attribute
            System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
        typeof(TestCustomValidation.CustomValidation.CustomRequiredAttribute),
        typeof(System.Web.Mvc.RequiredAttributeAdapter));
}
Bài liên quan
0