0

こんにちは皆さん、以前に同様の投稿を投稿しましたが、それは別の投稿です。今、Jquery コードで奇妙で奇妙な問題に直面しています。ここでは、Jquery を使用してコントローラー メソッドを呼び出していましたが、2 回呼び出しているため、データベースに 2 つのエントリが発生する可能性があります。これが私のJQueryで書いたものです

<script type="text/javascript">
    $('#btnSubmit').click(function () {
        var instructorUrl = '@Url.Action("ApplyToBecomeInstructor", "InstructorApplication")';
        var currentUser = '@Model.CurrentUserId';
        var user = [];
        var educationList = [];
        var experience = $('#Experience').val();
        var isWilling = $('#WillingToTravel').is(":checked");
        $('#editorRows .editorRow').each(function () {
            var education = {
                UniversityOrCollege: $(this).find('.university').val(),
                AreaOfStudy: $(this).find('.area').val(),
                Degree: $(this).find('.degree').val(),
                YearReceived: $(this).find('.year').val()
            }
            educationList.push(education);
        });
        var applicationFromView = {
            EducationalBackgrounds: educationList,
            CurrentUserId: currentUser,
            Experience: experience,
            WillingToTravel: isWilling
        }
        $.ajax({
            type: 'POST',
            url: instructorUrl,
            dataType: 'JSON',
            async: false,
            data: JSON.stringify(applicationFromView),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                return false;
            },
            error: function (data) {
            alert(xhr.status);
            alert(thrownError);
            alert(xhr.responseText);
                return false;
            }
        });
    });
</script>

私のコントローラーアクションは次のようになります

[HttpPost]
public ActionResult ApplyToBecomeInstructor(InstructorApplicationViewModel applicationFromView)
{
    Student thisStudent = this.db.Students.Where(o => o.StudentID == applicationFromView.CurrentUserId).FirstOrDefault();
    List<PaulSchool.Models.EducationalBackground> educationList = new List<EducationalBackground>();
    foreach (var educate in applicationFromView.EducationalBackgrounds)
    {
        var education = new Models.EducationalBackground
        {
            YearReceived = educate.YearReceived,
            Degree = educate.Degree,
            AreaOfStudy = educate.AreaOfStudy,
            UniversityOrCollege = educate.UniversityOrCollege
        };
        educationList.Add(education);
    }
    var instructorApplication = new InstructorApplication
    {
        BasicInfoGatheredFromProfile = thisStudent,
        Experience = applicationFromView.Experience,
        EducationalBackground = new List<Models.EducationalBackground>(),
        WillingToTravel = applicationFromView.WillingToTravel
    };
    instructorApplication.EducationalBackground.AddRange(educationList);
    this.db.InstructorApplication.Add(instructorApplication);
    this.db.SaveChanges();
    return this.Redirect("Index");
}

表示されるエラー メッセージは JSON Parsing error.. ですが、混乱を招きます。なぜこれが起こっているのか本当に疑問に思ったのですが、誰か見て助けてもらえますか?

4

2 に答える 2

1

これはあなたのコードが行うことです:

$('#btnSubmit').click(function () { // attach a click handler for the button.
...
...
// Look for elements inside the button... 
UniversityOrCollege: $(this).find('.university').val(), 

clickからに変更submit:

$('#formId').submit(function (e) { 
    ...
    // Now "this" is the form - not the button.
    // Look for elements inside the <form>
    UniversityOrCollege: $(this).find('.university').val(), 
    // Prevent the default form submition
    return false // Or: e.preventDefault();

別のヒント: jQueryserialize関数を使用します。

于 2012-06-10T05:14:09.873 に答える
0

$('#btnSubmit').click()ボタンを押すたびに発射されます。多くの場合、ユーザーはボタンを 1 回クリックするだけでよいのにダブルクリックしたり、何かが起こっていることを示していない場合、焦ってもう一度クリックしたりします。リクエストが行われたかどうかを判断するには、何らかの方法が必要です。これをクライアント側とサーバー側で行う方法があります。クライアント側で最も簡単な方法は、複数のクリックを防ぐためにボタンを無効にすることです。

$('#btnSubmit').click(function () {
    // Disable the button so it can't be clicked twice accidentally
    $('#btnSubmit').attr('disabled', 'disabled');
    //...
    $.ajax({
        //...
        complete: function() {
            // Make sure we re-enable the button on success or failure so it can be used again
            $('#btnSubmit').removeAttr('disabled');
        }
    });
});
于 2012-06-10T05:13:48.707 に答える