我正在开发一个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