user3187760
user3187760

Reputation: 11

unable to get list of elements in list

i have 2 model classes

public class ProductOptionRequest
{
    public string Name { set; get; }
    public List<ProductValuesRequest> productValues { get; set; }
}
public class ProductValuesRequest
{
    public string ValueName { get; set; }

}

public class ProductOptionValue
{

    public int OptionId { get; set; }
    public String ValueName { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }
}

and wrote one bs method and passing parameter value as value names. but I'm unable to get those values in a list object as productValues. May I know the solution, please.

public async Task<ReturnString> SaveProductOption(ProductOptionRequest request)
{
    request.productValues = new List<ProductValuesRequest>();
    foreach (ProductValuesRequest valueRequest in request.productValues)
    {
        ProductOptionValue res = new ProductOptionValue();
        res.ValueName = valueRequest.ValueName;
        object response = await productOptionValueRepository.InsertAsync(res, true);
    }
}

Upvotes: 0

Views: 91

Answers (2)

Mohammed Salaam
Mohammed Salaam

Reputation: 31

You are assigning an emplty list to productValues as request.productValues = new List() and trying to iterate the empty list.

Upvotes: 1

Amir Molaei
Amir Molaei

Reputation: 3820

In the first line of your method, you are replacing the productValues property of request object with a new empty list, :

 request.productValues = new List<ProductValuesRequest>();

Therefore, in foreach loop, you are iterating on an empty list.
Remove the first line and see if you still have any issues.

Upvotes: 1

Related Questions