使用jQuery和Ajax在局部视图中发回表单时的验证问题
本文关键字:表单 问题 验证 jQuery Ajax 视图 局部 使用 | 更新日期: 2023-09-27 18:10:23
我在ASP验证方面遇到了一些挑战。. NET MVC3项目时,使用jQuery和AJAX从部分视图发回数据。
将验证添加到我的局部视图中的NoteText字段,结果为"$('#noteAdd')"。提交事件未能触发和我的表单张贴直接到控制器。删除验证会导致预期的行为。
我希望有人能够在这里发生的事情,为什么会发生,我提供了一些关于如何解决这个问题的建议,我已经包括了我的部分,控制器,JS和视图下面。
<标题>我的部分[Bind(Include = "NoteId,NoteText,Date,SourceId,Username,TypeId,ItemId,Processed")]
[MetadataType(typeof(NotePartial_Validation))]
public class NotePartial
{
public int NoteId { get; set; }
public string NoteText { get; set; }
public DateTime? Date { get; set; }
public int? Source { get; set; }
public string Username { get; set; }
public int ItemId { get; set; }
public int TypeId { get; set; }
public IEnumerable<NotePartial> ExistingNotes { get; set; }
}
public class NotePartial_Validation
{
[HiddenInput(DisplayValue = false)]
public int NoteID { get; set; }
[Required]
public string NoteText { get; set; }
[HiddenInput(DisplayValue = false)]
public int ItemId { get; set; }
[HiddenInput(DisplayValue = false)]
public int TypeId { get; set; }
}
}
<标题> 我的控制器public class NoteController : Controller
{
[HttpPost]
public ActionResult Create(NotePartial model)
{
try
{
NoteMethods.CreateNote(model.NoteText, SessionManager.Current.ActiveUser.Username, model.ItemId, SessionManager.Current.ActiveUser.Company);
return Json(new { s = "Success" });
}
catch (NoPermissionException)
{
return Json(new { s = "No permission" });
}
}
}
<标题>我认为@model EF.NotePartial
@{using (Html.BeginForm("Create", "Note", new { area = "" }, FormMethod.Post, new { id = "noteAdd" }))
{
@Html.TextAreaFor(m => m.NoteText, new { @class = "note-input" }) //note-input
@Html.ValidationMessageFor(model => model.NoteText)
<input type="submit" value="Send" />
}}
<script type="text/javascript">
$(function () {
$('#noteAdd').submit(function () {
$.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);
}
});
// it is important to return false in order to
// cancel the default submission of the form
// and perform the AJAX call
return false;
});
});
标题>标题>标题>
将部分关键字放到类中:
public partial class NotePartial