Asp.net mvc 当使用Ajax.ActionLink加载部分控件时,客户端验证失败
我用Asp.net mvc 当使用Ajax.ActionLink加载部分控件时,客户端验证失败,asp.net-mvc,validation,Asp.net Mvc,Validation,我用Ajax.BeginForm实现了一个部分控件,它包含一个简单表单,其中包含一个文本框和验证消息。模型正在使用数据注释将此字段设置为必需 当使用Html.RenderPartial直接在视图中加载上述部分控件时,客户端验证工作正常 当使用Ajax.ActionLink加载上述部分控件时,客户端验证停止工作(表单使用空文本框提交,服务器端验证工作正常): 我在母版页上引用了以下javascript: <script src="/Scripts/MicrosoftAjax.js" type
Ajax.BeginForm
实现了一个部分控件,它包含一个简单表单,其中包含一个文本框和验证消息。模型正在使用数据注释将此字段设置为必需
当使用Html.RenderPartial
直接在视图中加载上述部分控件时,客户端验证工作正常
当使用Ajax.ActionLink
加载上述部分控件时,客户端验证停止工作(表单使用空文本框提交,服务器端验证工作正常):
我在母版页上引用了以下javascript:
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
控制器方法:
public ActionResult CreateNew()
{
return PartialView("JobNotesCreateNew", new NewJobNoteModel());
}
(二)
JobNotesCreateNew.ascx:
<% Html.EnableClientValidation();%>
<% using (Ajax.BeginForm("CreateNew", "JobNotes", FormMethod.Post, new AjaxOptions { UpdateTargetId = "JobNotes" }, new { id = "CreateNewJobNoteForm" }))
{ %>
<%: Html.ValidationSummary(true, "Please correct errors on the form.")%>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Note)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Note)%>
<%: Html.ValidationMessageFor(model => model.Note)%>
</div>
<p>
<input type="submit" value="Create" />
<%: Ajax.ActionLink("Cancel", "Cancel", "JobNotes", new AjaxOptions { UpdateTargetId = "CreateNewJobNote", HttpMethod = "GET" })%>
</p>
</fieldset>
<% } %>
所以我的问题是:为什么客户端验证会被破坏
谢谢
=========编辑:17/11/2010==============
有趣。
我正在模型上使用数据注释验证:
public class NewJobNoteModel
{
[Required]
public string Note { get; set; }
}
由于上面的验证在使用Html.BeginForm()时可以在客户端正常工作,所以我假设它也可以在Ajax.BeginForm()场景中工作。
我弄错了吗?我真的应该自己在客户端触发验证吗?
我将研究这篇文章,快速浏览一下,完整的功能在那里看起来相当手动,我希望那里的东西更加自动化
谢谢
=========编辑:25/11/2010==============
好的,我在这里找到了解决问题的方法:
AjaxLoadedContentScriptFix.js javascript对我来说似乎运行良好
这就是我一直在寻找的答案。
所有其他技巧,如在Ajax的AjaxOptions中使用自定义客户端验证。BeginForm-c'mon people:)@Maciek我需要查看更多代码才能做出正确的诊断。 然而,我想到了一些事情
- 首先,您可以使用 要调用的函数中的AjaxOptions Javascript OnSuccess或OnBegin-您可以像这样使用新的AjaxOptions{ OnBegin=“YourJavascriptFunction” }.具体地说,您的流程应该是这样的
- 其次,我注意到您没有使用post操作。有什么原因吗? 通常,在发送表单时,您希望执行Post而不是Get。 如果我是你,我会改变的。我在上面发布的代码段中更改了它
- 第三,你应该确保你的 JavaScript函数未调用 还不存在的控件
一旦输入正确的代码来执行验证,就可以在客户端检查数据。作为补充说明,我想补充一点,您应该确保您的流程也在服务器上进行验证。用户可以关闭JavaScript,如果您的代码没有在服务器上执行适当的检查,您将最终遇到问题 如原帖所述,以下是解决方案:
感谢您的回答,让我编辑我的问题以添加相关的代码示例。
<% Html.EnableClientValidation();%>
<% using (Ajax.BeginForm("CreateNew", "JobNotes", FormMethod.Post, new AjaxOptions { UpdateTargetId = "JobNotes" }, new { id = "CreateNewJobNoteForm" }))
{ %>
<%: Html.ValidationSummary(true, "Please correct errors on the form.")%>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Note)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Note)%>
<%: Html.ValidationMessageFor(model => model.Note)%>
</div>
<p>
<input type="submit" value="Create" />
<%: Ajax.ActionLink("Cancel", "Cancel", "JobNotes", new AjaxOptions { UpdateTargetId = "CreateNewJobNote", HttpMethod = "GET" })%>
</p>
</fieldset>
<% } %>
[HttpPost]
public ActionResult CreateNew(NewJobNoteModel newJobNote)
public class NewJobNoteModel
{
[Required]
public string Note { get; set; }
}