Home > other >  Update and Delete function not Working Asp.net MVC React
Update and Delete function not Working Asp.net MVC React

Time:01-01

i am creating the simple crud operation on Asp.net MVC Core along with React.i could add the records and view the records successfully. but i couldn't delete and update the records. i checked through postman it is working fine i could delete and update the records when i tested through React update and delete function are not working.what i tried so far i attached below.

Error show us on the console

Failed to load resource: the server responded with a status of 404 () this error is throwing on the console

StudentController.cs

     using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using webb.Model;

namespace webb.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly StudentDbContext _studentDbContext;


     
        [HttpPatch]
        [Route("UpdateStudent")]
        public async Task<Student> UpdateEmployee(Student objStudent)
        {
            _studentDbContext.Entry(objStudent).State = EntityState.Modified;
            await _studentDbContext.SaveChangesAsync();
            return objStudent;
        }

        [HttpDelete]
        [Route("DeleteStudent")]
        public bool DeleteEmployee(int id)
        {
            bool result = false;
            var student = _studentDbContext.Student.Find(id);
            if (student != null)
            {
                _studentDbContext.Entry(student).State = EntityState.Deleted;
                _studentDbContext.SaveChanges();
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }
    }
}

React Code

  async function DeleteEmployee(id) {
 

  await axios.delete("https://localhost:7205/api/Student/DeleteStudent"   id);
   alert("Employee deleted Successfully");
   Load();
  }
 
  async function update(event) {
    event.preventDefault();
    try {

  await axios.patch("https://localhost:7205/api/Student/UpdateStudent"  employees.find((u) => u.id === id).id || id,
        {
        id: id,
        stname: stname,
        course: course,
      
        }
      );
      alert("Registation Updateddddd");
     
      Load();
    } catch (err) {
      alert(err);
    }
  }
 
  return (
    

Check Through Swagger Update enter image description here

Delete enter image description here

Program.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using webb.Model;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


builder.Services.AddDbContext<StudentDbContext>(options =>
   options.UseSqlServer(builder.Configuration.GetConnectionString("StudentDbContext")));

var app = builder.Build();

app.UseCors(policy => policy.AllowAnyHeader()
                            .AllowAnyMethod()
                            .SetIsOriginAllowed(origin => true)
                            .AllowCredentials());


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

CodePudding user response:

like as said in comment:

if you call Delete api with query string of id you should set FromUri in api.

or you can change route to [Route("DeleteStudent/{id}")] and in front await axios.delete("https://localhost:7205/api/Student/DeleteStudent/" id);

  • Related