我正在参与一项大学管理项目,我试图编写一个条件来防止用户删除在其账户中有欠款的学生。如何防止删除已付款的学生?
一样,如果我想删除名为艾比约翰的学生,我应该不能,因为她有原因的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");
你只值分配给'ErrorMessage' ......环绕'尝试/ catch'返回'if' – FCin
即使您的ViewBag.ErrorMessage被设置为当前逻辑,您的try块仍然会被执行。所以你的服务器端验证没有太大的作用。在客户端大小上,您可以在剃须刀中使用条件来显示删除,如果那里有值的话。 –
好吧,我用'else {}'包围了'try/catch'语句,它的工作原理是不删除学生。但它没有显示错误信息,为什么它没有删除。它只是回到学生列表页面。 – Truecolor