【发布时间】:2021-03-10 15:33:59
【问题描述】:
如何编写控制器方法来获取 AsComponent 类中所有标记属性的总和。我想获得Ascomponents的总分。我想构建一个控制器方法,以便我可以将它与我的反应前端连接。
public class AsComponent
{
[Key]
[ScaffoldColumn(false)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AsID { get; set; }
public string Ascomponent { get; set; }
[ForeignKey("LOID")]
public int? Lid { get; set; }
public string LOID { get; set; }
[ForeignKey("POId")]
public string? POID { get; set; }
public int Marks { get; set; }
public string LD { get; set; }
public string Type { get; set; }
}
这是我的控制器。它是自动生成的,我已经做了一些编辑。
namespace GroupProject.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AsComponentsController : ControllerBase
{
private readonly ObeDbContext _context;
public AsComponentsController(ObeDbContext context)
{
_context = context;
}
// GET: api/AsComponents
[HttpGet]
public async Task<ActionResult<IEnumerable<AsComponent>>> GetAsComponents()
{
return await _context.AsComponents.ToListAsync();
}
// GET: api/AsComponents/5
[HttpGet("{id}")]
public async Task<ActionResult<AsComponent>> GetAsComponent(int id)
{
var asComponent = await _context.AsComponents.FindAsync(id);
if (asComponent == null)
{
return NotFound();
}
return asComponent;
}
// POST: api/AsComponents
[HttpPost]
public async Task<ActionResult<AsComponent>> PostAsComponent(AsComponent asComponent)
{
_context.AsComponents.Add(asComponent);
await _context.SaveChangesAsync();
return CreatedAtAction("GetAsComponent", new { id = asComponent.AsID }, asComponent);
}
// DELETE: api/AsComponents/5
[HttpDelete("{id}")]
public async Task<ActionResult<AsComponent>> DeleteAsComponent(int id)
{
var asComponent = await _context.AsComponents.FindAsync(id);
if (asComponent == null)
{
return NotFound();
}
_context.AsComponents.Remove(asComponent);
await _context.SaveChangesAsync();
return asComponent;
}
private bool AsComponentExists(int id)
{
return _context.AsComponents.Any(e => e.AsID == id);
}
}
}
【问题讨论】:
标签: c# asp.net-core asp.net-web-api