1

I'm new to asp.net MVC, I decided to build an ATM web APP without any database for learning purposes. I'm figuring out the MVC pattern, for the most part, I got it working but I need help with validating the entered withdrawal amount and displaying the correct error message if incorrect data is entered. Thanks.

Also, the logic where I check if the transactions the user has completed. ''' emp.TransactionBal <= 10 ''' but the condition keeps going down to 0, - 1, -2 and so on. But I want it to stop at 0. Thanks

    public class WithdrawController : Controller
    {
        WithdrawRepository rep = new WithdrawRepository();

        [BindProperty]
        public InputModel Input { get; set; }

        //
        // GET: /Withdraw/
        public ActionResult Index()
        {
            IEnumerable<Withdraw> obj = rep.SelectAllWithdraws();
            return View(obj);
        }

        
        // GET: /Withdraw/Create
        public ActionResult Create()
        {
            return View();
        }

       
        //
        // POST: /Withdraw/Create
        [HttpPost]
        
        [ValidateAntiForgeryToken]
        public ActionResult Create(Withdraw emp)
        {
            foreach (var obj in rep.SelectAllWithdraws())
            {
                emp.WithdrawId = obj.WithdrawId;
                emp.WithdrawDate = DateTime.Now;
                emp.TransactionBal = obj.TransactionBal;
                emp.AccountBal = obj.AccountBal;
                emp.User= obj.User;
                emp.UserID = obj.UserID;
            }
            try
            {
                //Check if user have enough cash for withdraw
                //Check if the transaction is not more than 1000
                //Check if the user have not exceeded 10 transaction that day

                if (ModelState.IsValid && (emp.AccountBal - Input.WithdrawAmt) > 0)
                {
                    if (Input.WithdrawAmt <= 1000 && emp.TransactionBal <= 10)
                    {
                            emp.WithdrawId++;
                            emp.TransactionBal--;
                            emp.AccountBal -= Input.WithdrawAmt;
                            rep.Add(emp);
                            return RedirectToAction("Index");
                    }
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to complete the transaction. " +
              "Try again, and if the problem persists " +
             "see your system administrator.");
                
            }
            return View();
        }

    

    public class InputModel: Withdraw 
    {
    }

Create.cshtml

<div class="row">
    <div class="col-md-6">
        <form method="post">
            
            <div class="mb-3">
                <label asp-for="WithdrawAmt">Amount</label>
                <input asp-for="WithdrawAmt" class="form-control" />
                <span asp-validation-for="WithdrawAmt" class = "text-danger"></span>
            </div>
                     

            <button class="btn btn-success">Create</button>
        </form>


    </div>
</div>

Model Class Withdraw.cs

public class Withdraw
    {
        public int WithdrawId { get; set; }
        
        [Required]        
        public double WithdrawAmt { get; set; }
        public int TransactionBal { get; set; }     
        public DateTime WithdrawDate { get; set; }

        public double AccountBal { get; set; }


    }
5
  • Please let me know if you need more info. Thanks again. Commented Jun 23, 2022 at 14:48
  • 1
    At the end of Create, I think you have to return the model: return View(emp); Commented Jun 23, 2022 at 15:01
  • 1
    catch { ModelState.AddModelError("", "Unable to complete the transaction. " + "Try again, and if the problem persists " + "see your system administrator."); } return View(); //here Commented Jun 23, 2022 at 15:39
  • Yes you have to return the view with the modelstate to get the errors to appear Commented Jun 23, 2022 at 16:01
  • Wondering if you could help with the logic, where I check if the transactions the user has completed. ''' emp.TransactionBal <= 10 ''' but the condition keeps going down to 0, - 1, -2 and so on. But I want it t stop at 0. Thanks Commented Jun 23, 2022 at 18:22

1 Answer 1

1

try adding error when your if condition is failing. something like this.

    try
    {
                //Check if user have enough cash for withdraw
                //Check if the transaction is not more than 1000
                //Check if the user have not exceeded 10 transaction that day

                if (ModelState.IsValid && (emp.AccountBal - Input.WithdrawAmt) > 0)
                {
                    if (Input.WithdrawAmt <= 1000 && emp.TransactionBal <= 10)
                    {
                            emp.WithdrawId++;
                            emp.TransactionBal--;
                            emp.AccountBal -= Input.WithdrawAmt;
                            rep.Add(emp);
                            return RedirectToAction("Index");
                    }
                }
                else
                {
                   ModelState.AddModelError("WithdrawAmt","Not enough balance")
                   return View(emp);
                }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you:) I added the else statement and it worked
Wondering if you could help with the logic, where I check if the transactions the user has completed. ''' emp.TransactionBal <= 10 ''' but the condition keeps going down to 0, - 1, -2 and so on. But I want it t stop at 0. Thanks
you can try this emp.TransactionBal = (emp.TransactionBal !=0) ? (emp.TransactionBal-1): emp.TransactionBal;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.