2012-01-06 152 views
0

我正在使用剃鬚刀中的mvc項目。在此我正在處理導入和導出模塊。 現在在我的行動中,我返回JSON對象。現在在Http Get Request或Http Post請求中,我遇到了同樣的問題。因爲斷點打了兩遍,我的記錄被添加到數據庫中兩次。在MVC中斷點兩次觸發

下面是我的腳本

<script type="text/javascript"> 
    $(document).ready(function (e) { 
     $(".update").live('click', function() { 
      var id = $(this).attr('id'); 
      var str = ""; 
      $(this).parent('td').parent('tr').find('td.data').each(function() { 
       str = str + ";" + $(this).text().trim(); 
      }); 
      if (str.length > 0) { 
       str = str.substring(1, str.length); 
      } 
      var header = $("#hdn_header").val(); 
      $.post('/ImportCSV/Update', { 
       ImportData: str, 
       ImportHeader: header 
      }, function (result) { 
       var CompName = result.CompanyName; 
       var CompPhone = result.CompanyPhone; 
       var CompEmail = result.CompanyEmail; 
       var CompWebsite = result.CompanyWebsite; 
       var CompAddress = result.CompanyAddress; 
       var CompZip = result.CompanyZip; 
       var CompCity = result.CompanyCity; 
       var CompCountry = result.CompanyCountry; 
       var CompNote = result.CompanyNote; 
       var ConFirstName = result.ConFirstName; 
       var ConLastName = result.ConLastName; 
       var ConTitle = result.ConTitle; 
       var ConPhone = result.ConPhone; 
       var ConEmail = result.ConEmail; 
       var ConNote = result.ConNote; 
       var ConMobile = result.ConMobile; 
       $(".update").each(function() { 
        if ($(this).attr('id') == id) { 
         var i = parseInt('1'); 
         $(this).parent('td').parent('tr').find('td.data').each(function() { 
          if ($(this).text().trim() != CompName && $(this).index() == 2) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompPhone && $(this).index() == 3) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompEmail && $(this).index() == 4) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompWebsite && $(this).index() == 5) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompAddress && $(this).index() == 6) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompZip && $(this).index() == 7) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompCity && $(this).index() == 8) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompCountry && $(this).index() == 9) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != CompNote && $(this).index() == 10) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != ConFirstName && $(this).index() == 12) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != ConLastName && $(this).index() == 13) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != ConTitle && $(this).index() == 14) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != ConPhone && $(this).index() == 15) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != ConMobile && $(this).index() == 16) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != ConEmail && $(this).index() == 17) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
          if ($(this).text().trim() != ConNote && $(this).index() == 18) { 
           { 
            $(this).css('backgroundColor', '#FFFFC0'); 
            return; 
           } 
          } 
         }); 
        } 
       }); 
      }); 
      return false; 
     }); 
    }); 
</script> 

以上是我的整個腳本,它再次被賦予相同的error.pls檢查,讓我知道我做錯了。 謝謝。

回答

1

這其中究竟是什麼你調用這個AJAX請求是不是很清楚,但我懷疑這是.click事件的一些錨或.submit按鈕內,你沒有取消這個默認的動作按鈕通過從處理程序返回false。所以,當鏈接被點擊時,您將獲得帶有AJAX POST的第一個請求,並在鏈接的默認操作執行後立即獲得。

因此,例如,如果你訂閱了.submit事件的某種形式的嘗試返回false取消默認動作:

$(function() { 
    $('#someForm').submit(function() { 
     $.post('/ImportCSV/Update', { ImportData: str, ImportHeader: header }, function (result) { 
      var CompName = result.CompanyName; 
     }); 

     return false; // <-- that's the important part 
    }); 
});