locked
Implement Delete Action in RestFul Web API in View using MVC RRS feed

  • Question

  • I have a following code for the Delete action in my controller.

    public HttpResponseMessage Delete(int id)
        {
            try
            {
                var course = TheRepository.GetCourse(id);
    
                if (course == null)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
    
                if (course.Enrollments.Count > 0)
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Can not delete course, students has enrollments in course.");
                }
    
                if (TheRepository.DeleteCourse(id) && TheRepository.SaveAll())
                {
                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
    
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }

    How can I implement the delete action in my ViewEngine with Delete button and ID field in my .cshtml file.

    Thanks in Advance.

    • Moved by Grady_Dong Tuesday, August 18, 2015 7:27 AM Off-Topic
    Monday, August 17, 2015 7:03 PM

Answers

All replies