C# MVC 4部分视图上的自定义验证don';不显示错误消息?

C# MVC 4部分视图上的自定义验证don';不显示错误消息?,c#,validation,asp.net-mvc-4,asp.net-mvc-partialview,error-messaging,C#,Validation,Asp.net Mvc 4,Asp.net Mvc Partialview,Error Messaging,当我单击索引页面上的提交按钮时,请参见下图: 我的问题: 我只是自定义必需属性,为什么不显示ProductDescription的错误消息 所有我的文件,请参见以下部分: ProductModel.cs namespace TestCustomValidation.Models { public class ProductModel { [Required(ErrorMessage = "It's for test for ProductName")]

当我单击索引页面上的提交按钮时,请参见下图:

我的问题: 我只是自定义必需属性,为什么不显示ProductDescription的错误消息

所有我的文件,请参见以下部分:

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; }
    }
}
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);
        }
}
namespace TestCustomValidation.CustomValidation
{
    public class CustomRequiredAttribute : RequiredAttribute
    {
    }
}
protected void Application_Start()
{
    // Add Custom Attribute
    System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
                typeof(TestCustomValidation.CustomValidation.CustomRequiredAttribute),
                typeof(System.Web.Mvc.RequiredAttributeAdapter));

}
index.cshtml

...
@{Html.RenderAction("Form");}
...
@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>
}
\u FormPartial.cshtml

...
@{Html.RenderAction("Form");}
...
@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>
}
CustomRequiredAttribute.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; }
    }
}
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);
        }
}
namespace TestCustomValidation.CustomValidation
{
    public class CustomRequiredAttribute : RequiredAttribute
    {
    }
}
protected void Application_Start()
{
    // Add Custom Attribute
    System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
                typeof(TestCustomValidation.CustomValidation.CustomRequiredAttribute),
                typeof(System.Web.Mvc.RequiredAttributeAdapter));

}

谢谢你的进步

我打赌您的页面中包含了jQuery验证,它从来不会真正进入服务器,而是显示与该输入字段关联的消息作为客户端验证。

我打赌您的页面中包含了jQuery验证,它从来不会真正进入服务器,而是显示与该输入字段关联的消息作为客户端验证。

我的答案,供进一步参考:

Global.asax.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; }
    }
}
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);
        }
}
namespace TestCustomValidation.CustomValidation
{
    public class CustomRequiredAttribute : RequiredAttribute
    {
    }
}
protected void Application_Start()
{
    // Add Custom Attribute
    System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
                typeof(TestCustomValidation.CustomValidation.CustomRequiredAttribute),
                typeof(System.Web.Mvc.RequiredAttributeAdapter));

}

我的答复,供进一步参考:

Global.asax.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; }
    }
}
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);
        }
}
namespace TestCustomValidation.CustomValidation
{
    public class CustomRequiredAttribute : RequiredAttribute
    {
    }
}
protected void Application_Start()
{
    // Add Custom Attribute
    System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
                typeof(TestCustomValidation.CustomValidation.CustomRequiredAttribute),
                typeof(System.Web.Mvc.RequiredAttributeAdapter));

}

您是否已在
global.asax.cs
中注册了
CustomRequiredAttribute
?但是这个属性的意义是什么,它有什么作用呢?哦,不@StephenMuecke,我错了,谢谢。我希望自定义必需属性使错误消息显示为图像,如
公共类ImageRequiredAttribute:RequiredAttribute{protected override ValidationResult IsValid(对象值,ValidationContext ValidationContext){return new ValidationResult(“”;}}
您是否已在
global.asax.cs
中注册了
CustomRequiredAttribute
?但是这个属性的意义是什么,它有什么作用呢?哦,不@StephenMuecke,我错了,谢谢。我希望自定义必需属性使错误消息显示为图像,如
公共类ImageRequiredAttribute:RequiredAttribute{protected override ValidationResult IsValid(对象值,ValidationContext ValidationContext){return new ValidationResult(“”;}}}