Asp.net mvc ASP.NET MVC 3中Ajax/jQuery回发的奇怪验证问题
我在ASP.NET MVC 3中的Ajax/jQuery回发中遇到问题Asp.net mvc ASP.NET MVC 3中Ajax/jQuery回发的奇怪验证问题,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我在ASP.NET MVC 3中的Ajax/jQuery回发中遇到问题 如果下面的代码验证失败,则会给出预期结果,并且表单不会发布 如果验证成功,则在提交表单时不会使用下面的函数,表单将提交一个json文件并返回给浏览器 如果有人能解释一下,我会很感激的 我已经在下面包括了我的部分、视图和控制器 我的看法 我的部分 名称空间XX { /// ///部分领导观点 /// [元数据类型(typeof(NoteAddPartial_Validation))] 公共部分类NoteAddParti
- 如果下面的代码验证失败,则会给出预期结果,并且表单不会发布
- 如果验证成功,则在提交表单时不会使用下面的函数,表单将提交一个json文件并返回给浏览器
名称空间XX
{
///
///部分领导观点
///
[元数据类型(typeof(NoteAddPartial_Validation))]
公共部分类NoteAddPartial
{
公共字符串注释\u文本{get;set;}
公共int?源{get;set;}
公共字符串用户名{get;set;}
公共int ItemId{get;set;}
公共int类型ID{get;set;}
}
公共类NoteAddPartial\u验证
{
[必需]
公共字符串注释\u文本{get;set;}
[HiddenInput(DisplayValue=false)]
公共int ItemId{get;set;}
[HiddenInput(DisplayValue=false)]
公共int类型ID{get;set;}
}
}
是否添加了客户端验证库
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
注意:
如果您将应用程序配置为不使用不引人注目的验证,则
不需要引用第二个库
是否添加了客户端验证库 jquery.validate.min.js jquery.validate.unobtrusive.min.js 注意: 如果您将应用程序配置为不使用不引人注目的验证,则 不需要引用第二个库
尝试附加到提交按钮上的
单击
事件,并将代码从此
更改为按钮父窗体的选择器。尝试附加到提交按钮上的单击
事件,并将代码从this
更改为按钮父窗体的选择器。我已包含这两个资产。$(this).valid()应该可以工作。是否捕捉到任何javascript错误。检查您的浏览器控制台是否有任何错误。这很有帮助。它们的引用出现在两个地方,都在脚本管理器上,并且是我手动添加的。再次感谢。我已经包含了这两项资产。$(this).valid()应该可以使用。是否捕捉到任何javascript错误。检查您的浏览器控制台是否有任何错误。这很有帮助。它们的引用出现在两个地方,都在脚本管理器上,并且是我手动添加的。再次感谢。在使用不引人注目的验证时,我无法拦截表单的提交。这就是你想做的,对吗?无论表单是否有效,您都希望在客户端javascript中拦截submit
事件并执行一些操作。在问题中说得更清楚可能会让你更快地得到一个好答案。在使用不引人注目的验证时,我无法拦截表单的提交。这就是你想做的,对吗?无论表单是否有效,您都希望在客户端javascript中拦截submit
事件并执行一些操作。把问题弄清楚可能会让你更快地得到一个好答案。
@model TheLayer.EF.NoteAddPartial
@{using (Html.BeginForm("Create", "Note", new { area = "" }, FormMethod.Post, new { id = "noteAdd" }))
{
@Html.TextAreaFor(m => m.Note_Text, new { @class = "note-input" }) //note-input
@Html.ValidationMessageFor(model => model.Note_Text)
<input type="submit" value="Send" />
}}
<script type="text/javascript">
$(function () {
$('#noteAdd').submit(function (e) {
e.preventDefault();
if ($(this).valid()) {
alert('posting');
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
error: function (xhr, ajaxOptions, thrownError) {
alert('An error occured when processing this request:\r\n\r\n' + thrownError);
},
success: function (result) {
alert(result.s);
}
});
}
return false;
});
});
public class NoteController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Create(NoteAddPartial model)
{
try
{
NoteMethods.CreateLeadNote(model.Note_Text, SessionManager.Current.ActiveUser.Username, model.ItemId, SessionManager.Current.ActiveUser.Company);
return Json(new { s = "Success" });
}
catch (NoPermissionException)
{
return Json(new { s = "No permission" });
}
}
}
namespace XX
{
/// <summary>
/// Partial lead person view
/// </summary>
[MetadataType(typeof(NoteAddPartial_Validation))]
public partial class NoteAddPartial
{
public string Note_Text { get; set; }
public int? Source { get; set; }
public string Username { get; set; }
public int ItemId { get; set; }
public int TypeId { get; set; }
}
public class NoteAddPartial_Validation
{
[Required]
public string Note_Text { get; set; }
[HiddenInput(DisplayValue = false)]
public int ItemId { get; set; }
[HiddenInput(DisplayValue = false)]
public int TypeId { get; set; }
}
}