Skip to content
Advertisement

Convert html foreach loop data to webgrid using asp.net mvc

Below html table is filling using two loop condition, and same i want using two loop conditions to fill Webgrid

I have tried to fill one field in web grid by using below code.

when I am click debugging mode after code line complete on this line

the following error is showing.

enter image description here

Advertisement

Answer

The exceptions you’ve included in your question are happening because you have not bound the WebGrid to a data source. You also don’t appear to have code that would spit out the table, so that would explain why you wouldn’t see any output from the table if that’s also an issue.

If you want to use WebGrid to display your data, you are going to have a much easier time if you simplify your objects first. I think the structure you’re working with is too complex/disconnected for it and made this a much more complicated problem than it should be.

It looks like you’re working with a structure something along these lines:

WebGrid is expecting a flat object more like this:

I made up a dataset that matches the structure you’ve described (though IMO parsing these as rows instead of columns from the original source would make more sense than transforming it just for display):

You can get to the flattened data model by doing the same style of nested looping you’re already doing:

And then display it using more appropriately simple view logic like this:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement