1

I have an MVC5 Inventory application that allows users to order supplies. The View shows the user the current 'on hand stock' for the item they wish to order. And they manually enter the quantity they wish to order.

What I want to do is stop them from submitting the view IF the number requested to order is greater than the number of stock on hand.

I first tried to handle this in jscript- and that worked, but the post still fires and so it warns the user about the issue but posts anyway. Not what I need.

Then I tried to do it in the controller:

This is the relevant code in my view

<div class="form-group">
@Html.Label("Units OnHand:", new { @class = "form-control" })
@Html.TextBox("OnHand", null, new { @class = "form-control", id = "OnHand", @readonly = "readonly" })
</div>

<!-- End Units OnHand-->

<!-- Begin Units Ordered-->
<div class="form-group">
@Html.Label("Qty of Units Ordered:", new { @class = "form-control" })
@Html.TextBox("UnitsOrdered", null, new { @class = "form-control", Value = ViewBag.Ordered, id = "UnitsOrdered", onblur = "stopit()"  })
</div>
 <input type="submit" id="addit" value="Add Supply To Request" class="btn btn-default" onclick="addSuppliesnow()" />

As you can see, I tried to fire the onblur = "stopit()" but that didn't work because it only fires after the next click on the page. And in this instance, the only other click is the submit button. So it fires and warns the user correctly but then it submits anyway.

Then I tried to do things in the controller. I passed the two values from the view (int OnHand, int UnitsOrdered)

int Ordered = UnitsOrdered;
int Onhand = UnitsOnHand;

if (Ordered > Onhand)

{
   return new EmptyResult();

}

else
{
     (save the data as usual . . . . code here)
}

In the above scenario, It fails to post/submit every time. Even when Ordered Amount is actually less than amount on hand. No matter what, I can't seem to hit the 'else' part.

What am I doing wrong here? Is there a better/easier way to get the results I need?

ExecChef
  • 387
  • 2
  • 13
  • Can you share the code of `addSuppliesnow()`? – Chetan Jun 16 '20 at 21:05
  • _Even when Ordered Amount is actually less than amount on hand_ that's opposite to the logic you have posted – JSteward Jun 16 '20 at 21:07
  • If the value is not correct you need to cancel the form submit. https://stackoverflow.com/questions/4227043/how-do-i-cancel-form-submission-in-submit-button-onclick-event – Chetan Jun 16 '20 at 21:08
  • If the else part isn't executing then either the method itself isn't executing or the if condition is true. That's pretty much it. My money is on the first one. – Scott Hannen Jun 16 '20 at 21:19
  • Can you attach a debugger and set a breakpoint entering the controller, to verify that you actually get the correct values from the client? If you would like to verify before sending to server, then you could use `onsubmit` on the [form element](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onsubmit), and if `ordered > onhand` then call event.preventDefault() – MatteKarla Jun 16 '20 at 21:09

0 Answers0