2014-06-24 58 views
0

我正在開發一個Asp.Net MVC應用程序,它處理使用Odata和LINQ的SQL數據庫上的CRUD操作。我想通過在控制器上創建一個動作來刪除數據庫中的SQL表中的所有數據如下創建綁定到集合的有效操作時出錯

服務器端代碼

private WhiteBoardAppContext db = new WhiteBoardAppContext(); 


public override HttpResponseMessage HandleUnmappedRequest(System.Web.Http.OData.Routing.ODataPath odataPath) 
     { 
      HttpResponseMessage emptyMSG = new HttpResponseMessage(); 
      switch (odataPath.Segments[2].ToString()) // kick out if the value is the same 
      { 


       case "DeleteSegment": 
            string Product = odataPath.Segments[1].ToString(); 
            byte[] param = new byte[Product.Length/2]; 
            for (int i = 0; i < param.Length; i++) 
            { 
             param[i] = Convert.ToByte(Product.Substring(i * 2, 2), 16); 
            } 
            Product = System.Text.Encoding.ASCII.GetString(param); 
            using (db) 
            { 
             var SegmentToDelete = from c in db.tblItems 
                    where c.Product == Product 
                    select c; 
             foreach (tblItem cr in SegmentToDelete) 
             { 
              db.tblItems.Remove(cr); 

             } 
             db.SaveChanges(); 
            } 

            return emptyMSG; 

       case "DeleteAll": 

            using (db) 
            { 
             var itemsToClear = from c in db.tblItems 
                    select c; 
             foreach (tblItem cr in itemsToClear) 
             { 
              db.tblItems.Remove(cr); 

             } 
             db.SaveChanges(); 
            } 

            return emptyMSG; 


default: 
        return base.HandleUnmappedRequest(odataPath); 
      } 
     } 

採用淘汰賽JS

self.deleteAll = function() { 
     var conf = confirm("Are you sure you want to delete all?"); 
     if (conf == true) { 
      $.ajax({ 
       url: '/odata/Items/DeleteAll' 
      }); 

     } 
    } 

客戶端代碼當我分配到一個按鈕點擊,它不工作,因爲我想它通過我錯誤如下

GET http://localhost:57044/odata/Canadiancrudes/DeleteAll 500 (Internal Server Error) 
Invalid action detected. 'DeleteAll' is not an action that can bind to 'Collection([WhiteBoardApp.Models.Item Nullable=False])'. 
+0

顯示完整的控制器代碼。 – haim770

+0

編輯問題 – DoIt

+0

'HandleUnmappedRequest'是控制器中的唯一動作嗎? – haim770

回答

0

簡單的錯誤,如下

self.deleteAll = function (item) { 
     var conf = confirm("Are you sure you want to delete all?"); 
     if (conf == true) { 
      $.ajax({ 
       url: '/odata/Items('+item.id+')/DeleteAll' 
      }); 

     } 
    } 
0
  1. 您需要在模型構建器定義動作DeleteAll,爲什麼它的行動不起作用自己固定的呢?如果一種方法有副作用,那麼它就是行動或功能。

    ODataConventionModelBuilder builder=new ODataConventionMocelBuilder 
    builder.EntitySet<Item>("Items"); 
    ActionConfiguration DeleteAll = modelBuilder.EntityType<Item>().Collection.Action("DeleteAll "); 
    
  2. 調用它與POST方法:

    POST ~/Items/DeleteAll 
    

這裏是一個行動的樣品,它可以幫助你。 https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataActionsSample/

注意:在您發佈的錯誤消息中,DeleteAll遵循Canadiancrudes,但js代碼顯示它應該遵循Items。你可能想檢查哪一個是正確的。