简体   繁体   English

使用jQuery和Ajax在部分视图中回发表单时的验证问题

[英]Validation problems when using jQuery and Ajax to post back form in partial view

I am having some challenges with validation in my ASP.NET MVC3 project when using jQuery and AJAX to post back data from a partial view. 当我使用jQuery和AJAX从部分视图回发数据时,我在ASP.NET MVC3项目中验证时遇到了一些挑战。

Adding validation to the NoteText field within my partial view results in "$('#noteAdd').submit" event failing to trigger and my form posting directly to the controller. 在部分视图中向NoteText字段添加验证会导致“$('#noteAdd')。submit”事件无法触发,我的表单直接发布到控制器。 Removing the validation results in the expected behaviour. 删除验证结果的预期行为。

I was hoping somebody might be able to shed some light over what's happening here, why it's happening and my provide some advice on how to resolve the issue and I've included my partial, controller, JS and view below. 我希望有人能够对这里发生的事情有所了解,为什么会发生这种情况,我提供了一些如何解决问题的建议,我已经将我的部分,控制器,JS和视图包括在内。

My Partial 我的偏爱

[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; }

}

} }

My Controller 我的控制器

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" }); 
        }

    }
}

My View 我的观点

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM