MVC3、Ajax 和具有客户端非侵入式验证的模式表单
我无法将标题中的内容组合在一起。我读过各种问题,但没有找到一个涵盖我想做的一切的问题。
我正在使用 jqModal 作为我的模式窗口。我正在使用 Ajax.BeginForm。我的模型用 [Required]
、[Display(...)]
等装饰。
这些东西都使用 @Html.RenderAction 放置在页面上(...)
根据我在 ajax 调用中使用 OnBegin
还是 OnComplete
,我要么得不到客户端验证(更新就不会) t take),或者模态陷入这样的状态整个页面是“模态化”的,没有对话框。但实际上我想要的是,如果存在客户端或服务器端验证错误,模式将保持在原位。
如果没有可用/启用 JavaScript,该网站不需要任何功能即可工作。
head:
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery.cleditor.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jqModal.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.cleditor.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.timeago.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jqModal.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/functions.js")"></script>
模型:
public class ReplacedTextModel {
public ReplacedTextModel() { }
public ReplacedTextModel(int id, string text, string replaced) {
Id = id;
Text = text;
Replaced = replaced;
}
public int Id { get; set; }
[Required, Display(Description = "The text of the question", Prompt = "Please enter the question text."), UIHint("MultilineString")]
public string Text { get; set; }
public string Replaced { get; set; }
}
编辑器模板(其中包含一些扩展方法,但与问题无关,所以请忽略):
@model string
<div class="form-element">
@Html.Label("", new Dictionary<string, object> { { "title", Html.Description("") } })
@if (Html.IsRequired()) {
<span class="form-required">*</span>
}
<div class="form-control">
@Html.TextArea("", Model, new Dictionary<string, object> { { "title", Html.Prompt("") } })
<div class="form-validation">
@Html.ValidationMessage("")
</div>
</div>
</div>
视图(一个页面上可能有很多这些,并且可能会重复,所以我正在使用指南来实现唯一性):
@model ReplacedTextModel
@{
var id = Guid.NewGuid().ToString("N");
var windowId = "id_" + id + "_window";
var triggerId = "id_" + id + "_trigger";
var replaceId = "id_" + id + "_replace";
var functionId = "id_" + id + "_closeForm";
}
<div id="@replaceId">
@Html.Raw(Model.Replaced)
<span class="edit-mode"><a id="@triggerId" href="#">edit</a></span>
<div id="@windowId" class="jqmWindow" style="display: none">
@using (Ajax.BeginForm("Text", new AjaxOptions { UpdateTargetId = replaceId, OnBegin = functionId, HttpMethod = "POST" })) {
@Html.HiddenFor(x => x.Id)
@Html.EditorFor(x => x.Text)
<input type="submit" value="Save" />
}
<div><a href="#" class="jqmClose">Cancel</a></div>
</div>
<script type="text/javascript">
$(function () { $("#@windowId").jqm({ trigger: "#@triggerId" }); });
function @(functionId)() {
// THIS IS A PROBLEM
// if it's not called or is called on OnBegin then the modal doesn't close properly
// if OnComplete is used, the dialog disappears but the validation doesn't work
// in either case, validation failure on backend (try posting whitespace) doesn't show validation errors.
$("#@windowId").jqmHide();
};
</script>
</div>
控制器方法:
public ActionResult Text(int id) {
return PartialView("Text", ReplacedTextModel.Get(id));
}
[HttpPost]
public ActionResult Text(int id, string text) {
if (ModelState.IsValid) {
try {
if (string.IsNullOrWhiteSpace(text)) throw new ArgumentException("No text was provided for the question.", "text");
ReplacedTextModel.Update(id, text);
} catch (Exception ex) {
ModelState.AddModelError(ex);
}
}
return Text(id);
}
很抱歉这篇文章很长,但我看到其他人提到这不是一件坏事!如果需要,很乐意发布任何附加代码/信息。我可能错过了一些东西,因为我从项目中的几个位置复制/粘贴。
提前致谢。
I can't get the combination of things in the title to work together. I've read various questions but not found one that covers everything I'm trying to do.
I'm using jqModal for my modal window. I'm using Ajax.BeginForm. My models are decorated with [Required]
, [Display(...)]
etc.
This stuff is all being placed on a page using @Html.RenderAction(...)
Depending on whether I use OnBegin
or OnComplete
in the ajax call, I either get no client-side validation (the update just doesn't take), or the modal gets stuck in a state where the whole page is "modalled out" without a dialog in place. But actually what I want is for the modal to stay in place if there is either a client or server side validation error.
The site does not require any functionality to work without javascript being available/enabled.
head:
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery.cleditor.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jqModal.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.cleditor.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.timeago.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jqModal.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/functions.js")"></script>
The model:
public class ReplacedTextModel {
public ReplacedTextModel() { }
public ReplacedTextModel(int id, string text, string replaced) {
Id = id;
Text = text;
Replaced = replaced;
}
public int Id { get; set; }
[Required, Display(Description = "The text of the question", Prompt = "Please enter the question text."), UIHint("MultilineString")]
public string Text { get; set; }
public string Replaced { get; set; }
}
The editor template (this contains some extension methods but they're not relevant to the problem, so please ignore):
@model string
<div class="form-element">
@Html.Label("", new Dictionary<string, object> { { "title", Html.Description("") } })
@if (Html.IsRequired()) {
<span class="form-required">*</span>
}
<div class="form-control">
@Html.TextArea("", Model, new Dictionary<string, object> { { "title", Html.Prompt("") } })
<div class="form-validation">
@Html.ValidationMessage("")
</div>
</div>
</div>
The view (there may be many of these on a page, and they may be repeated, so I'm using the Guids for uniqueness):
@model ReplacedTextModel
@{
var id = Guid.NewGuid().ToString("N");
var windowId = "id_" + id + "_window";
var triggerId = "id_" + id + "_trigger";
var replaceId = "id_" + id + "_replace";
var functionId = "id_" + id + "_closeForm";
}
<div id="@replaceId">
@Html.Raw(Model.Replaced)
<span class="edit-mode"><a id="@triggerId" href="#">edit</a></span>
<div id="@windowId" class="jqmWindow" style="display: none">
@using (Ajax.BeginForm("Text", new AjaxOptions { UpdateTargetId = replaceId, OnBegin = functionId, HttpMethod = "POST" })) {
@Html.HiddenFor(x => x.Id)
@Html.EditorFor(x => x.Text)
<input type="submit" value="Save" />
}
<div><a href="#" class="jqmClose">Cancel</a></div>
</div>
<script type="text/javascript">
$(function () { $("#@windowId").jqm({ trigger: "#@triggerId" }); });
function @(functionId)() {
// THIS IS A PROBLEM
// if it's not called or is called on OnBegin then the modal doesn't close properly
// if OnComplete is used, the dialog disappears but the validation doesn't work
// in either case, validation failure on backend (try posting whitespace) doesn't show validation errors.
$("#@windowId").jqmHide();
};
</script>
</div>
The controller methods:
public ActionResult Text(int id) {
return PartialView("Text", ReplacedTextModel.Get(id));
}
[HttpPost]
public ActionResult Text(int id, string text) {
if (ModelState.IsValid) {
try {
if (string.IsNullOrWhiteSpace(text)) throw new ArgumentException("No text was provided for the question.", "text");
ReplacedTextModel.Update(id, text);
} catch (Exception ex) {
ModelState.AddModelError(ex);
}
}
return Text(id);
}
Sorry for the long post, but I've seen others mention that it's not a bad thing! Happy to post any additional code/info if requested. I may have missed something as I have copy/pasted from a few locations in the project.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论