jquery 对话框中的服务器端验证
抱歉我的语言 - 我只能阅读英语:)
我想在 asp.net mvc 中做这样的事情:
1.向用户显示一个页面
2.打开模态对话框(jquery-ui)并显示部分视图
3.在客户端验证用户输入数据
4.如果没问题,则验证服务器上的输入数据
5a.如果没问题的话我想重新加载页面
5b.如果有错误我想向用户显示它
6.用户可以随时使用按钮关闭对话框。
我有宽度 5a 和 6 的问题。
在 Firefox 中,当我进行服务器验证并单击关闭按钮(对话框('close'))时,当我重定向到调用验证数据的页面时。如果我单击对话框标题中的“x”,则关闭即可。在歌剧中也是同样的情况。
在 Firefox 中,当我在服务器传递对话框上插入良好的数据和验证时,附加功能不会关闭(它在 Opera 中工作)。
我在 MVC 方面没有丰富的经验,不知道我做得是否正确。请查看我的代码并告诉我它是否错误,我不应该这样做。
控制器代码:
public ActionResult Create()<br/>
{
return PartialView(new UserDTO());
}
[HttpPost]
public ActionResult Create(UserDTO model)
{
if(model.Email != "[email protected]")
{
ModelState.AddModelError("Email", "wrong email");
return PartialView(model);
}
return new EmptyResult();
}
//索引页上的javascript
<script type="text/javascript">
var dialog;
$(document).ready(function () {
dialog = $("#divInsert").dialog({
title: "Insert",
resizable: false,
modal: true,
autoOpen: false
});
$("#aShowInsert").click(function () {
$("#divInsert").empty();
$("#divInsert").load("Home/Create", function () {
$("#inputCloseModal").click(function () {
dialog.dialog('close');
return false;
});
});
dialog.dialog("open");
return false;
});
});
</script>
<div id="divInsert" /> - its a dive where i loads form.
<a id="aShowInsert">add element</a> - link thats open dialog.
我按顺序导入js文件:jquery-1.6.1.js,jquery-ui-1.8.13.js,jquery.validate.js,jquery.validate。 unobtrusive.js
表单视图如下所示:
// import js..
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Surname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Surname)
@Html.ValidationMessageFor(model => model.Surname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" id="inputSubmit" />
<input type="submit" value="Close" id="inputCloseModal" />
</p>
}
<script type="text/javascript">
$("#inputSubmit").click(function (e) {
e.preventDefault();
var form = $("#divInsert form");
form.validate();
if (form.valid()) {
$.ajax({
url: "/Home/Create",
data: form.serialize(),
type: "POST",
success: function (data) {
if (data === "") {
location.reload();
}
else {
$("#divInsert").html(data);
$.validator.unobtrusive.parse($("#divInsert"));
}
}
});
}
return false;
});
sorry for my language - in English i can only read :)
i want to do in asp.net mvc something like this:
1. show user a page
2. open modal dialog (jquery-ui) and show partial view
3. validate user input data on client side
4. if it's OK then validate input data on server
5a. if it's OK then i want reload page
5b. if there is a errors i want show its to user
6. user can close dialog at any time with button on it.
i have problem width 5a and 6.
in Firefox when i do server validate and click close button (dialog('close')) when i get redirect to page that was call to validate data. if i click 'x' in header of dialog box it's close OK. In opera it's the same situation.
additional in Firefox when i insert a good data and validation on server pass dialog box don't close (it's work in opera).
i don't have big experience in mvc and don't know if i do it right. please look at my code and tell me if it's wrong and i shouldn't do it that way.
controler code:
public ActionResult Create()<br/>
{
return PartialView(new UserDTO());
}
[HttpPost]
public ActionResult Create(UserDTO model)
{
if(model.Email != "[email protected]")
{
ModelState.AddModelError("Email", "wrong email");
return PartialView(model);
}
return new EmptyResult();
}
// javascript on index page
<script type="text/javascript">
var dialog;
$(document).ready(function () {
dialog = $("#divInsert").dialog({
title: "Insert",
resizable: false,
modal: true,
autoOpen: false
});
$("#aShowInsert").click(function () {
$("#divInsert").empty();
$("#divInsert").load("Home/Create", function () {
$("#inputCloseModal").click(function () {
dialog.dialog('close');
return false;
});
});
dialog.dialog("open");
return false;
});
});
</script>
<div id="divInsert" /> - its a dive where i loads form.
<a id="aShowInsert">add element</a> - link thats open dialog.
I import js files in order: jquery-1.6.1.js, jquery-ui-1.8.13.js, jquery.validate.js, jquery.validate.unobtrusive.js
the form view looks like that:
// import js..
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Surname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Surname)
@Html.ValidationMessageFor(model => model.Surname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" id="inputSubmit" />
<input type="submit" value="Close" id="inputCloseModal" />
</p>
}
<script type="text/javascript">
$("#inputSubmit").click(function (e) {
e.preventDefault();
var form = $("#divInsert form");
form.validate();
if (form.valid()) {
$.ajax({
url: "/Home/Create",
data: form.serialize(),
type: "POST",
success: function (data) {
if (data === "") {
location.reload();
}
else {
$("#divInsert").html(data);
$.validator.unobtrusive.parse($("#divInsert"));
}
}
});
}
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呵呵。
这就是我喜欢编程的原因。
解决方案非常简单,我将其发布在这里以避免其他人花一天时间搜索该错误。
在我的解决方案中,当我重新加载对话框的内容时,我忘记将函数附加到按钮关闭。这就是为什么在服务器验证重定向到此操作(主页/创建)后单击关闭按钮的原因。
在这段代码之后,我添加了这段代码及其工作原理。
heh.
this is why i love programming.
the solution is very simple and i post it here to avoid other people to spent a day searching that bug.
in my solution i forget to attach function to button close when i reload content of the dialog box. this is why click on the close button after server validation redirect to this action (Home/Create).
after this code i add this code and its working.