我想在ASP.NET MVC中创建删除功能。我不太确定如何使用IHttpActionResult在ASP.NET.MVC中创建Delete函数。 (sql)
,dBConnection.SubjectCodes.Remove(subjectCodeList);
中存在错误
dBConnection.SaveChanges();
。
该错误表明:
数据库连接不包含SubjectCodes的定义
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Data;
using System.Linq;
using SubjectAPI.Models;
namespace SubjectAPI.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
[RoutePrefix("api/Subject")]
public class SubjectController : ApiController
{
public List<SubjectCode> SubjectCodes;
[ActionName("DeleteSubject")]
[Route("DeleteSubject")]
public IHttpActionResult DeleteSubject(int Subject_Code_ID)
{
DBConnection dBConnection = new DBConnection();
DataTable subjectCodeList = dBConnection.GetSqlDataTable(sql);
dBConnection.SubjectCodes.Remove(subjectCodeList);
dBConnection.SaveChanges();
return Ok(subjectCodeList);
}
}
}
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
namespace SubjectAPI.Models
{
public class SubjectCode
{
public int Class_Code_ID { get; set; }
public string Subject_Code { get; set; }
public string Subject_Name { get; set; }
public Nullable<bool> Delete_Status { get; set; }
}
}
答案 0 :(得分:0)
DeleteSubject方法采用一个int Subject_Code_ID
作为参数,但是好像您正在尝试删除一个subjectCodeList
。尝试获取与Subject_Code_ID
匹配的单个主题代码,然后删除该主题代码。
编辑:我需要查看SubjectCode和DBConnection类为您提供一个具体示例。根据这些工作的方式,您可能需要以下内容:
DataTable subjectCodeList = dBConnection.GetSqlDataTable(sql);
var subjectCodeToDelete = subjectCodeList.Rows.Find(Subject_Code_ID);
if (subjectCodeToDelete == null)
{
return NotFound();
}
dBConnection.SubjectCodes.Remove(subjectCodeToDelete);
dBConnection.SaveChanges();