1

I am trying to build HTML in my view :

<div class="row contentHeaderInfo">
     @foreach (var groupedItems in Model.DSet.GroupBy(a => new {a.rowId}))
     {             
        <ul>
            @foreach (var item in groupedItems)
            {
                 <li>@item.Label  @item.labelValue</li>
            }
        </ul>            
     }
</div>

Data inside the object where rowid = 1 :

groupedItems[0] = {1,"FName","ABC"}
groupedItems[0] = {1,"LaName","DEF"}
groupedItems[0] = {1,"Address",""}
groupedItems[0] = {1,"City","LosAngeles"}
groupedItems[0] = {1,"State","California"}
groupedItems[0] = {1,"Zip","90045"}
groupedItems[0] = {1,"Street","Cardiff"}
groupedItems[0] = {1,"House No","1234"}
groupedItems[0] = {1,"Apt","1"}

For certain label I want to put more than one labelValues inside an <a> tag.

For example If @item.Label = "Address" then I want

 <li> Address <a href ="blah"> 1234 Cardiff LosAngeles California 90045 </a></li>

And the rest (apt, house no,city, state, zip) so not appear individually. Is it possible to achieve this with lambda expression?

1 Answer 1

1

Why not use @if?

@foreach (var item in groupedItems)
{
    @if (item.Label == "zip")
    {
        <li>@item.Label  @item.labelValue</li>
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think lambdas or linq would be a good solution, because the elements might not come in in the order you expect. Its probably better to process the object in the code behind before trying to insert them in the HTML.
thank u. sure it makes perfect sense to do this in the code behind.
You could build the string with groupedItems.Singe(item => item.Label == "House No").labelValue + .... But processing the object in the code behind would be better.

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.