Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting Two ViewBag Values

Not sure if this is the right way to go about this. But, I have two textboxes on a view that are populated by Viewbag from the controller. They are int values. One Value is the Number of units in stock (Viewbag.stock) and the other is the number of units ordered (Viewbag.ordered). I have a third textbox that I want to show the value of Viewbag.Stock - Viewbag.Ordered.

I tried doing this in the controller, but that didn't bring back a result. I also tried doing this in the value of the textbox, and that also did not work.

This is the first way that I tried to accomplish this

Model

        public int? OnHand { get; set; }
        public int? UnitsOrdered { get; set; }

controller

        ViewBag.Stock = r.FirstOrDefault().OnHand;
        ViewBag.Ordered = r.FirstOrDefault().UnitsOrdered;
        ViewBag.Total = ViewBag.Stock - ViewBag.Ordered;

View

        <div class="form-group">
        @Html.LabelFor(model => model.UnitsOrdered, (string)"Units On Hand ", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">              
            @Html.TextBox("OnHand", null, new { @class = "form-control", Value = ViewBag.Stock, @readonly = "readonly" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UnitsOrdered, (string)"Number of Units Ordered", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UnitsOrdered, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            @ViewBag.MyCountIntro @ViewBag.MyCount
            @Html.ValidationMessageFor(model => model.UnitsOrdered, "", new { @class = "text-danger" })

        </div>
    </div>

            <div class="col-md-10">
            @Html.TextBox("Remaining", null, new { @class = "form-control", Value = ViewBag.Total, @readonly = "readonly" })
            </div>

In the above scenario, Viewbag.Stock is 60. Viewbag.Ordered is 2. Viewbag.Total should be 58. But instead, it is blank

I have also tried to calculate this in the view, itself. Which also yields a blank.

            <div class="col-md-10">
            @Html.TextBox("Remaining", null, new { @class = "form-control", Value = ViewBag.Stock - Viewbag.Ordered, @readonly = "readonly" })
        </div>
like image 588
ExecChef Avatar asked Jun 30 '26 12:06

ExecChef


2 Answers

ViewBag itself will treat the value as an object. You should cast that object to appropriate type before doing your calculation.

ViewBag.Total = (int)ViewBag.Stock - (int)ViewBag.Ordered;

like image 176
Adrian Avatar answered Jul 02 '26 02:07

Adrian


You could add a view model instead of using the ViewBag (see ViewModels or ViewBag?):

class MyViewModel { 
    public int Ordered {get; set;}
    public int Stock {get; set;}
    public int Total => Stock - Ordered;
}

This will be a much more maintainable code since it'll allow easy refactoring, typed member and so on.

like image 43
Thomas Ayoub Avatar answered Jul 02 '26 01:07

Thomas Ayoub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!