如何防止删除付款到期的学生?

时间:2017-02-03 18:52:51

标签: c# asp.net-mvc

我正在进行大学管理项目,我正在尝试编写一个条件,以防止用户删除在其帐户中有到期付款的学生。

enter image description here

如果我想删除名为Abby John的学生,我应该不能,因为她有50美元的付款。但是使用我尝试编写的代码,它会被删除而不会出现任何错误消息/警告。

我在 StudentController 中编辑了 POST删除方法,但它无效。亲切的任何提示或帮助请。

[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);
        }

2 个答案:

答案 0 :(得分:1)

您当前的代码对ErrorMessage逻辑没有任何作用,您可以这样做:

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 :(得分:1)

即使您的业务逻辑失败,您仍在执行代码以删除学生。如果您使用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");
}