1

私はインデックスページで[送信]ボタンをクリックすると、以下の画像を参照してください。 index pageMVC 4パーシャルビューのカスタム検証でエラーメッセージが表示されませんか?

私の質問:私は、カスタム属性を必要としています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; } 
    } 
} 

index.cshtml

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

_FormPartial.cshtml

01:

すべての私のファイルは、以下のセクションを参照してください

@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> 
} 

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); 
     } 
} 

CustomRequiredAttribute.cs事前のための

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

ありがとう!

+1

あなたはglobal.asax.cs' ''であなたのCustomRequiredAttribute'を登録しましたか?しかし、その属性のポイントは何ですか?それは何ですか? –

+0

Oh No @StephenMuecke、私の間違い、ありがとう。私は、カスタムの必要な属性は 'パブリッククラスImageRequiredAttributeのように、画像などのエラーメッセージのショーを作りたい:RequiredAttribute { 保護オーバーライドValidationResult IsValid関数(オブジェクト値、ValidationContext validationContext) {新しいValidationResult(「」)を返す 。 } } ' – Elliwood

答えて

0

私の答えは、さらに参考のために:

Global.asax.cs

protected void Application_Start() 
{ 
    // Add Custom Attribute 
    System.Web.Mvc.DataAnnotationsModelValidatorProvider.RegisterAdapter(
       typeof(TestCustomValidation.CustomValidation.CustomRequiredAttribute), 
       typeof(System.Web.Mvc.RequiredAttributeAdapter)); 

} 
0

あなたのページにjQueryの検証が含まれていて、実際にあなたのサーバーに行くことはありませんが、代わりにその入力フィールドに関連付けられたメッセージをクライアント側の検証として表示します。

関連する問題