2014-06-24 31 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。你可能想检查哪一个是正确的。