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?