In my controller, I have a post method that takes the information from a form, and writes it to the database. Before I SaveChanges to the database I would like to foreach through the parameter object that is being passed into the method. The object is called item and is Item type.
When I try to foreach through the object item I receive an error,
foreach statement cannot operate on variables of type 'Item' because 'Item' does not contain a public instance defenition for 'GetEnumerator'
I have more experience in the Mean stack so I am not used to receiving type errors for my algorithms. I came up with a solution using a bunch of if statements but I would rather have cleaner code using a foreach statement.
This is an example of two of my if statements.
if (item.CsCost == null)
{
item.CsCost = 555555555;
}
if (item.Department == null)
{
item.Department = "Null";
}
In my immediate window if I type ?item. This is the object information that is displayed. It is a key value pair Dictionary object. In relation to the database, the keys are the column names, and the value is all the information in the row.
? item
{App.Models.Item}
Approved: null
Brand: "Disco"
CsCost: null
CsPack: 40
DateApproved: null
DateRequested: {8/3/2018 11:53:45 AM}
DateReviewed: null
Department: null
Deposit: 0.00
DepositUpc: null
Description: "BANANAS"
FoodStampable: true
GM: null
ID: null
ItemCode: null
ItemType: "Disco"
LineExtension: null
Margin: null
Merchandiser: null
NPrice: 0.0
Notes: null
PkQty: 1.00
PluRequest: false
Qty: 1
RequestType: "Disco"
RequestedBy: "jschroeder"
Reviewed: null
ScaleTare: 0.00
ScaleUoM: "LB"
Section: "50300001"
Selected: false
Size: 1.00
SizeAlpha: "1lb"
Store: null
StoreFix: null
TOB: null
UOM: "LB"
UPC: "4011"
UnCost: null
UnitSize: 1.0000
Vendor: "701 "
Wicable: null
This is kind of a two part question regarding type and type errors.
Once I can start foreaching through the object, I would like to find every value which is null. This I know how to do. Here is what I don't know how to do.
Once I find the null value of the key value pair, I need to alter the value to 5555555 if the column type is int or decimal, and "Null" if the column type is string.
Here is the sudo code for what I would like to do.
function(item){
foreach(x in item){
find the value that is null
if this value is a string then replace with "Null"
if this value is int then replace with 5555555
}
}
So for example, after this algorithm is complete, the Notes field, item.notes will be {..., Notes: "Null", ...}