我无法将标题中的内容组合在一起工作。我已经阅读了各种问题但未找到涵盖我正在尝试的所有问题的问题。
我正在使用jqModal作为我的模态窗口。我正在使用Ajax.BeginForm。我的模型装饰有[Required]
,[Display(...)]
等。
这些内容全部放在使用@Html.RenderAction(...)
取决于我是否在ajax调用中使用OnBegin
或OnComplete
,我要么没有得到客户端验证(更新只是没有),要么模态陷入状态在没有对话框的情况下整个页面被“修改掉”的地方。但实际上我想要的是,如果存在客户端或服务器端验证错误,模式将保持不变。
如果没有javascript可用/启用,该网站不需要任何功能。
头:
<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>
视图(页面上可能有很多这些视图,并且可能会重复这些视图,所以我使用Guids来表示唯一性):
@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);
}
对不起,很长的帖子,但我看到其他人提到这不是一件坏事!很高兴在需要时发布任何其他代码/信息。我可能已经遗漏了一些东西,因为我已经从项目中的几个位置进行了复制/粘贴。
提前致谢。