0
I have this Action in my controller
[HttpGet]
public ActionResult Details(AzureDiscountGroupModel modelD)
{
var discount = _azureDiscountGroupService.GetAll();
var list = new ResellerListModel();
var resellers = _resellerService.QueryAll()
.Include(r => r.WhiteLabels)
.ToList();
foreach(var item in resellers)
{
list.Id = item.Id;
list.Name = item.Name;
list.Alias = item.Alias;
list.MpnId = item.ResellerMpnId;
//list.CreatedOn = item.AcceptContractDate;
//list.WhiteLabel = item.WhiteLabels
}
ViewBag.Desconto = discount.Where(x => x.Id > 0);
ViewBag.DetailReseller = resellers.Where(x => x.AzureDiscountGroupId == modelD.Id);
return View(modelD);
}
I’m filling out this table like this
@foreach (var item in ViewBag.DetailReseller as IEnumerable<ResellerListModel>)
{
<tr align="center">
<td>
@item.Name
</td>
<td>
@item.Alias
</td>
<td>
@item.WhiteLabel
</td>
<td>
@item.MpnId
</td>
<td>
@item.CreatedOn
</td>
</tr>
}
The moment I try to fill in, I get this mistake
System.Nullreferenceexception: 'Object reference not set to an instance of an object.'
How do I iterate through this model?
EDIT1
I made that change, to see if I can
[HttpGet]
public ActionResult Details(AzureDiscountGroupModel modelD)
{
var discount = _azureDiscountGroupService.GetAll();
var list = new List<ResellerListModel>();
var resellers = _resellerService.QueryAll()
.Include(r => r.WhiteLabels)
.ToList();
foreach(var item in resellers)
{
list.Add(CreateListModelFrom(item));
}
ViewBag.Desconto = discount.Where(x => x.Id > 0);
ViewBag.DetailReseller = resellers.Where(x => x.AzureDiscountGroupId == modelD.Id);
return View(modelD);
}
and this one
private ResellerListModel CreateListModelFrom(Reseller reseller)
{
var model = new ResellerListModel();
model.Id = reseller.Id;
model.Name = reseller.Name;
model.Alias = reseller.Alias;
model.MpnId = reseller.ResellerMpnId;
model.WhiteLabel = true;
var firstWhiteLabel = reseller.WhiteLabels.OrderBy(x => x.CreatedOn).FirstOrDefault();
model.CreatedOn = firstWhiteLabel.CreatedOn.ToLocalTime();
return model;
}
And yet I can’t iterate through the list in cshtml
If I do so, the list is filled in, but I miss the items that should be shown in the table
@foreach (var item in ViewBag.DetailReseller as IEnumerable<List<ResellerListModel>>)
But if I do so, it is null error
@foreach (var item in ViewBag.DetailReseller as List<ResellerListModel>)
Apparently correct. Try to materialize the list using a . Tolist() when you arrow the data in
ViewBag.DetailReseller = resellers.Where(x => x.AzureDiscountGroupId == modelD.Id);
and make sure the data is filled.by debugging this line.– Julio Borges
Putting a break, I saw that the Viewbag is null due to this here:
ViewBag.DetailReseller = list.Where(x => x.AzureDiscountGroupId == modelD.Id);
. I’m not populating this field in the model:AzureDiscountGroupId
– pnet
Probably the popular field the query will return data.
– Julio Borges
So, the Azurediscountgroupid field is fixed at 2 and I don’t know why this and even setting the model.id == 2 still gives error. The lambda is not working and need to bring the screen ID to the method
– pnet
You missed a Tolist() in the Viewbag lambda, but I need to understand why the fixed field is coming in 2
– pnet