模型验证在 jquery 对话框中不起作用
我正在使用 MVC3 并添加了具有所需属性的模型验证。然后我创建了具有 jquery 对话框(不是 ajax 对话框)的页面。在这种情况下验证不起作用。但如果我把 html 从对话框放到页面上,它就可以正常工作。
有谁知道如何解决问题吗?
这是我的 JavaScript:
$(document).ready(function () {
$("#registerDialog").dialog({ autoOpen: false, show: "blind", hide: "explode", modal: true, resizable: false, height: 570, width: 390 });
$(".headerButton").button();
$(".accountBtn").button();
$('ul').roundabout({ autoplay: 'false', autoplayDuration: 3000 });
$("#registerBtn").click(function () {
$("#registerDialog").dialog("open"); return false; });
$("#closeRegisterDialog").click(function () { $("#registerDialog").dialog("close");
});
$("#registerBtnSbmt").click(function () {
$("#registerForm").submit(); return false; });
})
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "registerForm" }))
{
<div id="registerDialog" title="Регистрация">
@Html.LabelFor(x => x.FirstName)
<br/>
@Html.TextBoxFor(x => x.FirstName, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.FirstName)
<br/>
@Html.LabelFor(x => x.LastName)
<br/>
@Html.TextBoxFor(x => x.LastName, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.LastName)
<br/>
@Html.LabelFor(x => x.Email)
<br/>
@Html.TextBoxFor(x => x.Email, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.Email)
<br/>
@Html.LabelFor(x => x.Password)
<br/>
@Html.TextBoxFor(x => x.Password, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.Password)
<br/>
@Html.LabelFor(x => x.ConfirmPassword)
<br/>
@Html.TextBoxFor(x => x.ConfirmPassword, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.ConfirmPassword)
<br/>
@Html.CheckBoxFor(x => x.RememberYou) запомнить Вас?
<br/>
<br/>
@Html.ActionLink("Сохранить", "LogIn", "Account", null, new { @class = "accountBtn", style = "font-size: 0.8em;", id = "registerBtnSbmt" })
<a href="#" class="accountBtn" id="closeRegisterDialog" style = "font-size: 0.8em;">Закрыть</a>
</div>
}
I am using MVC3 and added model validation with required attributes. Then I have created page which has jquery dialog (not ajax dialog). Validation does not work in that case. But if I put html from dialog to page it works fine.
Does any body know how to solve problem?
Here is my JavaScript:
$(document).ready(function () {
$("#registerDialog").dialog({ autoOpen: false, show: "blind", hide: "explode", modal: true, resizable: false, height: 570, width: 390 });
$(".headerButton").button();
$(".accountBtn").button();
$('ul').roundabout({ autoplay: 'false', autoplayDuration: 3000 });
$("#registerBtn").click(function () {
$("#registerDialog").dialog("open"); return false; });
$("#closeRegisterDialog").click(function () { $("#registerDialog").dialog("close");
});
$("#registerBtnSbmt").click(function () {
$("#registerForm").submit(); return false; });
})
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "registerForm" }))
{
<div id="registerDialog" title="Регистрация">
@Html.LabelFor(x => x.FirstName)
<br/>
@Html.TextBoxFor(x => x.FirstName, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.FirstName)
<br/>
@Html.LabelFor(x => x.LastName)
<br/>
@Html.TextBoxFor(x => x.LastName, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.LastName)
<br/>
@Html.LabelFor(x => x.Email)
<br/>
@Html.TextBoxFor(x => x.Email, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.Email)
<br/>
@Html.LabelFor(x => x.Password)
<br/>
@Html.TextBoxFor(x => x.Password, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.Password)
<br/>
@Html.LabelFor(x => x.ConfirmPassword)
<br/>
@Html.TextBoxFor(x => x.ConfirmPassword, new { @class = "registerUser" })
<br/>
@Html.ValidationMessageFor(x => x.ConfirmPassword)
<br/>
@Html.CheckBoxFor(x => x.RememberYou) запомнить Вас?
<br/>
<br/>
@Html.ActionLink("Сохранить", "LogIn", "Account", null, new { @class = "accountBtn", style = "font-size: 0.8em;", id = "registerBtnSbmt" })
<a href="#" class="accountBtn" id="closeRegisterDialog" style = "font-size: 0.8em;">Закрыть</a>
</div>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将表单标签移动到对话框内。您会注意到,当您创建对话框并打开它时,它会将您的 div 移出表单。您可能会想要:
的底部有一个解释为什么会发生这种情况
页面 :
You need to move your form tags inside of your dialog. You will notice when you create the dialog and open it, it moves your div out of your form. You would want:
There is an explanation of why this happens at the bottom of this page
From the page:
首先抱歉之前没有回答,所以,转到答案。
注意:承认您需要了解有关创建模态窗口的知识,让我们开始代码:(我使用的是 ASP.NET MVC 5,它适用于 HTML 5x,旧版本不行
) sstatic.net/5Ui87.jpg" rel="nofollow noreferrer">
结果是:
Firstly sorry for not answered before, so, go to the answer.
Note: Admitting that you gotta knowledge about create a modal window let's go the code: (I'm use ASP.NET MVC 5, it's works for HTML 5x, to old version nope)
That's result: