Answered by:
Validate that sum of textbox are equal

Question
-
User-1461584407 posted
In my view I have 2 columns of textboxes that are used to collect decimal values. When submit is processed, I want the controller to add up the values from the 2 columns of textboxes and then compare the totals with each other. if the totals are equal, the controller will submit the data to the database, if they're not equal, I want the controller to return the view with an error message.
This is what I have in my controller, however, it's not what I want.
[HttpPost] public ActionResult Create(CIP collection) { if (ModelState.IsValid) { collection.EstimateTotal = collection.Admin + collection.Services + collection.Construction; collection.AllocationTotal = collection.FY+ collection.FY1 + collection.FY2+ collection.FY3; if (collection.EstimateTotal != collection.AllocationTotal) { throw new ArgumentException("Totals must be equal", "EstimateTotal"); } }
Monday, March 14, 2011 12:32 PM
Answers
-
User-1461584407 posted
I asked for help too soon.
I achieved returning the view with an error message by using ModelState.AddModelError() like so,
if (collection.EstimateTotal != collection.AllocationTotal) { ModelState.AddModelError("", "Totals must be equal"); return View(collection); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 14, 2011 12:59 PM
All replies
-
User-610330605 posted
What do you get with your current code?
What is the deviation from the expected result?
Monday, March 14, 2011 12:45 PM -
User-1461584407 posted
I just want to validate that the sums of the 2 columns are equal, if they are not, then return the view with an error message saying that the totals of the 2 columns must be equal.
Monday, March 14, 2011 12:46 PM -
User-610330605 posted
I just want to validate that the sums of the 2 columns are equal, if they are not, then return the view with an error message saying that the totals of the 2 columns must be equal.
You already mentioned that. What is happening now?
Monday, March 14, 2011 12:56 PM -
User-1461584407 posted
I asked for help too soon.
I achieved returning the view with an error message by using ModelState.AddModelError() like so,
if (collection.EstimateTotal != collection.AllocationTotal) { ModelState.AddModelError("", "Totals must be equal"); return View(collection); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 14, 2011 12:59 PM