ExecChef
ExecChef

Reputation: 449

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>

Upvotes: 3

Views: 629

Answers (2)

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

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.

Upvotes: 3

Adrian
Adrian

Reputation: 8597

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;

Upvotes: 3

Related Questions