我正在開發一個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])'.
顯示完整的控制器代碼。 – haim770
編輯問題 – DoIt
'HandleUnmappedRequest'是控制器中的唯一動作嗎? – haim770