2013-12-13 6 views
0

숙제, ASP.NET MVC 및 Web 양식 웹 응용 프로그램 (데이터베이스없이 하드 코딩 된 데이터 포함)을 사용하여 의사를 평가합니다. 사용자는 항목을 편집하고 삭제할 수 있어야하지만 컨트롤러의 "삭제"링크는 "삭제"페이지로 이동하지 않습니다. 그러나 "삭제"링크 위로 마우스를 가져 가면 URI는 다음과 같습니다. DoctorApplication/Delete? DoctorPicture = Images/0.cropped.jpg. 응용 프로그램 전체 int ID 대신 문자열 DoctorPicture 사용하고 이미지 경로없이 0.cropped 이미지/0.cropped 경로를 생각 해요 내 삭제보기가 표시되지 않는 이유는 내 TestDoctorRepository, DoctorPicture = "Images/0cropped.jpg"를 사용하면 이미지를 페이지에 표시 할 수 있습니다. '삭제'보기가 표시되지 않는 이유는 무엇입니까? DoctorApplicationController에서컨트롤러에서 "삭제"링크가 "삭제"페이지로 이동하지 않습니다.

public TestDoctorRepository() 
     { 
      doctors = new List<Doctor> { 
       new Doctor { DoctorPicture = "Images/0cropped.jpg", DoctorName = "Michael Shores", DoctorSpecialty = "Opthamology", times = 0, rating = 0, rated = true, avg=0, fave = true }, 
      // more code 

      }; 
     } 

public void Remove(string DoctorPicture) 
     { 
      var doctor = from d in doctors where d.DoctorPicture == DoctorPicture select d; 
      doctors.Remove(doctor.FirstOrDefault()); 
     } 

:

public ActionResult Delete(string DoctorPicture) 
     { 
      repo.Remove(DoctorPicture); 
      return RedirectToAction("Index"); 
     } 

     //private ActionResult View(Func<List<Doctor>> func) 
     //{ 
     // throw new NotImplementedException(); 
     //} 

     // 
     // POST: /DoctorApplication/Delete/5 

     [HttpPost] 
     public ActionResult Delete(string DoctorPicture, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add delete logic here 
       repo.Remove(DoctorPicture); 
       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 

답변

0

페이지 대신 전용 삭제보기로 색인 작업에 당신을 리디렉션에서 컨트롤러의 첫 Delete 기능. POST Delete 메서드에서 try-catch 블록 빼기와 똑같은 작업을 수행하고 있습니다. 삭제 된보기 이름을 가리 키도록 변경해야합니다.

MVC 페이지의 URL은 해당 링크를 클릭 할 때 실행될 작업을 가리 킵니다. 올바른 동작 (귀하의 Delete 호출)을 실행하고 있지만 컨트롤러의 색인 작업 (삭제 페이지로 이동하지 않음)으로 이동해야합니다. 보고있는 내용은 정확합니다 : Delete으로 가고 있지만 Delete은 색인으로 리디렉션 중입니다. (문자열 DoctorPicture) { repo.Remove (DoctorPicture를) 삭제 공공 ActionResult;

+0

나는에 삭제 방법을 변경 return RedirectToAction ("Delete"); } 및 [HttpPost] 공공 ActionResult은 (문자열 DoctorPicture, FormCollection 수집을) 삭제 는 { 는 { // TODO을 시도해보십시오 로직 여기 repo.Remove (DoctorPicture)을 삭제 추가; return RedirectToAction ("Delete"); } catch { return View(); } } 하지만 실행했을 때 ... – Grafica

+0

오류로 인해 "서버가이 주소에 대한 요청을 완료하지 못하는 방식으로 리디렉션 중입니다."라는 오류가 발생했습니다. – Grafica

+0

그 자체로 리디렉션 할 수 없기 때문입니다. 틀린 return 문을 사용하고 있습니다. 'RedirectToAction'은 컨트롤러에서 특정 메소드를 호출하는 것과 본질적으로 같습니다. 따라서'RedirectToAction ("Delete")를 가지는'Delete' 메소드는 자신을 계속 호출 할 것이기 때문에 결코 끝나지 않을 것입니다. '리턴 뷰 ("Delete"); '와 같은 것이 필요합니다. – IronMan84

관련 문제