2016-06-22 19 views
0

custom validationAJAXpopupの中でMVCに実行する必要があります。私はCustomValidatorを作成し、IsValid()メソッドをオーバーライドしています。ポップアップがカスタム検証を適切にレンダリングしないという問題があります。mVCのAJAXポップアップでのカスタム検証

マイコード:

_Layout.cshtml

<!DOCTYPE html> 
<html> 
<head> 
    @Styles.Render("~/Content/css") 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" /> 
    @Scripts.Render("~/bundles/jquery") 
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
    <script src="~/Scripts/jquery.validate.min.js"></script> 
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 
    <script src="https://cdn.jsdelivr.net/jquery.ajax.unobtrusive/3.2.4/jquery.unobtrusive-ajax.min.js"></script> 
    @Scripts.Render("~/bundles/modernizr") 
    <script> 
     $(function() { 
      $("#modalDialog").dialog({ 
       autoOpen:false, 
       width: 600, 
       height: 300, 
       modal: true 
      }); 
      $("#opener").click(function() { 
       $("#modalDialog").dialog("open"); 
      }); 
     }); 

     function OnSuccess(response) 
     { 
      $("#modalDialog").text(response); 
     } 
    </script> 
</head> 

Index.cshtml:

@model MvcPopup.Controllers.HomeController.SomeModel 
@{ViewBag.Title = "Home Page";} 
<p>This is page content !!!</p> 
<button id="opener">Open Dialog</button> 
<div id="modalDialog" title="Basic Modal Dialog"> 
    <p>This is a basic modal dialog</p> 
    @using (Ajax.BeginForm("Index", new AjaxOptions { UpdateTargetId = "ID", OnSuccess = "OnSuccess" })) 
    { 
    <div> 
     <fieldset> 
      <legend>Info</legend> 
      @Html.LabelFor(m => m.EmailAddress) 
      @Html.TextBoxFor(m => m.EmailAddress) 
      @Html.ValidationMessageFor(m => m.EmailAddress) 
      <input type="submit" value="Submit" /> 
     </fieldset> 
    </div> 
    } 
</div> 

HomeController.cs:

public class HomeController : Controller 
    { 
     public class SomeModel 
     { 
      [CustomValidator] 
      [Display(Name = "Email address")] 
      public string EmailAddress { get; set; } 
     } 
     public ActionResult Index() 
     { 
      var model = new SomeModel(); 
      return View(); 
     } 
     [HttpPost] 
     public ActionResult Index(SomeModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       return PartialView(); 
      } 
      return PartialView(); 
     } 

CustomValidator.cs:

public class CustomValidator : ValidationAttribute 
    { 
     protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
     { 
      if (value != null) 
      { 
       if (value.ToString().Contains("X")) 
       { 
        return ValidationResult.Success; ; 
       } 
       else 
       { 
        return new ValidationResult("Enter an email address with letter X"); 
       } 
      } 
      else 
      { 
       return new ValidationResult("" + validationContext.DisplayName + " is mandatory.Please enter it."); 
      } 
     } 
    } 

私はCustom Validationsが発射されていることがわかりますが、ポップアップページのレンダリングには少し曲がっています。以下のように表示されます。

On Load

さて、私は電子メールtextboxの任意の値を入力して、Submitにクリックしないでください、私は私のカスタム検証を見ることができる「Email Address is mandatory. Please enter it.」ポップアップページに発砲し、明らかであるます以下のように:

Afterwards

私は意味、そのカスタム検証は、電子メールtextboxの近くに表示される必要があり、物事はpopupのようになりますことはかなり明白。助けてください。

Validations

EDITはpopupページに表示されますが、内容が多少曲がっています。 I mean the content of the parent page too appear on the popup page。以下のスナップショット。どのようにそれを取り除く?助けてください。

enter image description here

+0

'$( "#modalDialog")HTML(レスポンス);'( 'ない、テキスト()') –

+1

理由だけではなく、 ' "X" が'含まれていることを確認するために、 'RegularExpressionAttribute'を使用しない - そこに。この属性の実際のポイントはありません –

+0

@StephenMuecke:このXインクルードはデモンストレーションの目的にすぎません。私は他の有効な種類のカスタム検証を行うことができます。 – Anurag

答えて

0

これは典型的な、多くの場合、かなりの時間を見ています。検証が失敗しなかった理由は、モーダルダイアログのようなコンテンツが動的であり、後でページに追加/登場するためです。フォームの検証では、フォームを再度解析して、ページに動的に追加されたコントロールを検証する必要があります。

OnSuccess()を設定しているため、フォームを解析するために使用できます。

function OnSuccess() 
{ 
    if($.validator) 
    { 
     $.validator.unobtrusive.parse("form"); 
    } 

    $("#modalDialog").html(response); 
} 

Visit here詳細については、

このソリューションは、検証が正常に動作していることを前提としています。

+0

検証属性は 'IClientValidatable'を実装していないので、再解析は必要ありません(無意味な余分なオーバーヘッド) –

+0

この場合、意図した通りに動作しません。 – Rohit416

+0

@ Rohit .. try ..thanks – Anurag

関連する問題