custom validation
をAJAX
popup
の中で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
が発射されていることがわかりますが、ポップアップページのレンダリングには少し曲がっています。以下のように表示されます。
さて、私は電子メールtextbox
の任意の値を入力して、Submit
にクリックしないでください、私は私のカスタム検証を見ることができる「Email Address is mandatory. Please enter it.
」ポップアップページに発砲し、明らかであるます以下のように:
私は意味、そのカスタム検証は、電子メールtextbox
の近くに表示される必要があり、物事はpopup
のようになりますことはかなり明白。助けてください。
Validations
EDITはpopup
ページに表示されますが、内容が多少曲がっています。 I mean the content of the parent page too appear on the popup page
。以下のスナップショット。どのようにそれを取り除く?助けてください。
'$( "#modalDialog")HTML(レスポンス);'( 'ない、テキスト()') –
理由だけではなく、 ' "X" が'含まれていることを確認するために、 'RegularExpressionAttribute'を使用しない - そこに。この属性の実際のポイントはありません –
@StephenMuecke:このXインクルードはデモンストレーションの目的にすぎません。私は他の有効な種類のカスタム検証を行うことができます。 – Anurag