2017-02-03 22 views
-2

我正在参与一项大学管理项目,我试图编写一个条件来防止用户删除在其账户中有欠款的学生。如何防止删除已付款的学生?

enter image description here

一样,如果我想删除名为艾比约翰的学生,我应该不能,因为她有原因的50 $付款。但是随着我试图编写的代码,它会被删除而没有任何错误消息/警告。

我编辑了POST删除方法在StudentController,但它不工作。请提供任何提示或帮助,好心。即使没有你的业务逻辑后

var paymentDue = false; 
if (s.PaymentDue > 0) 
{ 
    ViewBag.ErrorMessage = "Student has overdue payment. Need to CLEAR payment before deletion!"; 
    paymentDue = true; 
} 

if(!paymentDue) 
{ 
    try 
    { 
     Student student = studentRepository.GetStudentByID(id); 
     studentRepository.DeleteStudent(id); 
     studentRepository.Save(); 
    } 
    catch (DataException /* dex */) 
    { 
     //Log the error (uncomment dex variable name after DataException and add a line here to write a log. 
     return RedirectToAction("Delete", new { id = id, saveChangesError = true }); 
    } 
} 

return RedirectToAction("Index"); 
+1

你只值分配给'ErrorMessage' ......环绕'尝试/ catch'返回'if' – FCin

+1

即使您的ViewBag.ErrorMessage被设置为当前逻辑,您的try块仍然会被执行。所以你的服务器端验证没有太大的作用。在客户端大小上,您可以在剃须刀中使用条件来显示删除,如果那里有值的话。 –

+0

好吧,我用'else {}'包围了'try/catch'语句,它的工作原理是不删除学生。但它没有显示错误信息,为什么它没有删除。它只是回到学生列表页面。 – Truecolor

回答

1

您当前的代码没有与ErrorMessage逻辑东西,你可以做这样的事情。如果您使用RedirectToAction,则会丢失ViewBag数据。这将是一个简单的方法来填充你的ViewBag.ErrorMessage值后返回了同样的观点:与其他`{}`声明或

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Delete(int id) 
{ 
    Student s = studentRepository.GetStudentByID(id); 

    if (s.PaymentDue > 0) 
    { 
     ViewBag.ErrorMessage = "Student has overdue payment. Need to CLEAR payment before deletion!"; 
     // Assuming that you are using Student object to populate your delete view 
     return View(s); 
    } 

    try 
    { 
     Student student = studentRepository.GetStudentByID(id); 
     studentRepository.DeleteStudent(id); 
     studentRepository.Save(); 
    } 
    catch (DataException /* dex */) 
    { 
     //Log the error (uncomment dex variable name after DataException and add a line here to write a log. 
     return RedirectToAction("Delete", new { id = id, saveChangesError = true }); 
    } 
    return RedirectToAction("Index"); 
} 
+0

我认为你的逻辑是相反的。我想你的意思是'if(!paymentDue)' –

+0

你绝对没错@ChrisDunaway,我修好了。谢谢! – awh112

+2

由于重定向,“ViewBag”数据将会丢失。 –

1

您正在执行的代码删除学生:

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Delete(int id) 
     { 
      Student s = studentRepository.GetStudentByID(id); 

      if (s.PaymentDue > 0) 
      { 
       ViewBag.ErrorMessage = "Student has overdue payment. Need to CLEAR payment before deletion!"; 
      } 
      try 
      { 
       Student student = studentRepository.GetStudentByID(id); 
       studentRepository.DeleteStudent(id); 
       studentRepository.Save(); 
      } 
      catch (DataException /* dex */) 
      { 
       //Log the error (uncomment dex variable name after DataException and add a line here to write a log. 
       return RedirectToAction("Delete", new { id = id, saveChangesError = true }); 
      } 
      return RedirectToAction("Index"); 
     } 

// GET: /Student/Delete/5 
     public ActionResult Delete(bool? saveChangesError = false, int id = 0) 
     { 
      if (saveChangesError.GetValueOrDefault()) 
      { 
       ViewBag.ErrorMessage = "Delete failed. Try again, and if the problem persists see your system administrator."; 
      } 
      Student student = studentRepository.GetStudentByID(id); 
      return View(student); 
     } 
+0

不知道如何填充原始视图,这也将失败。 – Nkosi

+0

@Nkosi真。他需要在View中传递用于填充视图的模型/值。 – rageit

+0

你是对的。 – Nkosi