0

I need to include value from object which comes from controller via ViewBag inside the <label> and <input> html tag. The following is which I used inside the view page:

foreach (var stockItem in ViewBag.joinqry2)
{
      <div class="col-md-2">
          <label style='font-weight:normal;' name='InstockID' value= 'stockItem.ItemName' ><span> @(stockItem.ItemName) </span></label>
          <input class='form-control input_field stockItem' name='labOrder.ConsumedInventories[ + i + ].ConsumedQuantity' type='number' id='" + data[i].Instock + "' min='0' value= '"+ stockItem.ConsumedQuantity + "'/>
     </div>
}

ViewBag.joinqry2:

{ ConsumedQuantity = 1, ItemName = "Streck Tube" }
{ ConsumedQuantity = 1, ItemName = "Bubble Wrap" }
{ ConsumedQuantity = 7000, ItemName = "Biohazard Bag" }
{ ConsumedQuantity = 1, ItemName = "Absorbent Papers" } 
{ ConsumedQuantity = 1, ItemName = "Test Tube" }    

StockItem contains ConsumedQuantity and ItemName values inside the foreach loop but I am still getting error like this below:

My error is:

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in App_Web_euru3gao.dll but was not handled in user code

Additional information: 'object' does not contain a definition for 'ItemName'

4
  • Check your ViewBag is properly filled or not Commented Oct 7, 2016 at 11:53
  • @BharatPatidar .. yes it is filled correctly ... I even checked inside the foreach and each stockItem carries individual ItemName and ConsumedQuantity data Commented Oct 7, 2016 at 12:06
  • Just as an FYI this is considered bad practice.. you need to create a strongly-typed ViewModel for this. It will assist in maintaining the code later down the line and is generally cleaner... but anyway... what is the class name that holds the properties ConsumedQuantity and ItemName? Commented Oct 7, 2016 at 12:12
  • @BviLLe_Kid... thanks for the info.. I try your method... and ViewBag.joinqry2 is generated from the joining two tables.. that is ConsumedQuantity and ItemName are from two different tables Commented Oct 7, 2016 at 12:39

1 Answer 1

2

You have to cast the item retrieved from the ViewBag. In your current implementation, stockItem is an object, therefore it does not contain a property ItemName.

As BviLLe_Kid mentioned, the cleaner way is to use a ViewModel:

public class ConsumedItemModel {
    public int ConsumedQuantity {get; set;}
    public string ItemName {get; set;}
}

In your cshtml file, use the @model directive to tell Razor which ViewModel to expect. If you need additional properties, create a ViewModel that also contains the collection of ConsumedItemModel as a property. The following assumes that you only pass the collection of ConsumedItemModel from the controller.

@model IList<ConsumedItemModel>
foreach (var stockItem in Model) {
    var name = stockItem.ItemName;
    var qty = stockItem.ConsumedQuantity;
    // render label and input
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.