Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:55:29+00:00 2026-06-07T05:55:29+00:00

I have this code in a view <ul> @foreach (var tag in Model) {

  • 0

I have this code in a view

<ul>
    @foreach (var tag in Model)
    {
        <li><a href="/Post/Tag/@tag.Id">@tag.Name</a></li>
    }
</ul>

now I need to group List Items by its first character, like

A
 -Apple
 -Ant

C
 -Car

S
 -Sky
 -Sea
 -Sun

How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:55:30+00:00Added an answer on June 7, 2026 at 5:55 am

    How can I achieve this?

    Very easy. The answer, as in the 99.99% of the questions in the asp.net-mvc tag is always the same: use view models.

    I assume that you have the following domain model:

    public class Tag
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    So as always you start by defining a view model that will meet the requirements you want to implement in this view (which is grouping a list of Tag domain models by the first letter of their Name property and display a link):

    public class TagViewModel
    {
        public string Letter { get; set; }
        public IEnumerable<Tag> Tags { get; set; }
    }
    

    then you will obviously have a controller whose responsibility is to query your DAL layer in order to fetch the domain model, build a view model and finally pass this view model to the view:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // Get the domain model
            var tags = new[]
            {
                // Guess this comes from a database or something
                new Tag { Id = 1, Name = "Apple" },
                new Tag { Id = 2, Name = "Ant" },
                new Tag { Id = 3, Name = "Car" },
                new Tag { Id = 4, Name = "Sky" },
                new Tag { Id = 5, Name = "Sea" },
                new Tag { Id = 6, Name = "Sun" },
            };
    
            // now build the view model:
            var model = tags.GroupBy(t => t.Name.Substring(0, 1)).Select(g => new TagViewModel
            {
                Letter = g.Key,
                Tags = g
            });
    
            return View(model);
        }
    }
    

    and finally a view:

    @model IEnumerable<TagViewModel>
    
    @foreach (var item in Model)
    {
        <h2>@item.Letter</h2>
        <ul>
            @foreach (var tag in item.Tags)
            {
                <li>
                    <!-- Please notice the usage of an HTML helper to generate
                         the anchor instead of the hardcoded url shown in your
                         question which is very bad
                    -->
                    @Html.ActionLink(
                        tag.Name, 
                        "Post", 
                        "Tag", 
                        new { id = tag.Id }, 
                        null
                    )
                </li>
            }
        </ul>
    }
    

    which will obviously give the desired result:

    enter image description here

    So next time you encounter some difficulty or problem in ASP.NET MVC tell to yourself: I must use a view model. See, problem solved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have this code in my view.. <%=Html.DropDownList(ProductTemplate,new SelectList(Model.ProductTemplate,Value,Text))%> I know if this
Say I have this line of code in the view. <?php echo CHtml::activeTextField($model,'start_time'); ?>
I have the following code in an MVC 3 view <% foreach (var stockItem
Hi I have a ModelBinded View foreach (var Model in Model) { @Html.RadioButtonFor(modelItem =>
I have this code in my view: echo form_label('State', 'state'); $options = array( 'No
I have this code: tableList = [[NSMutableArray alloc] initWithObjects:@First View,@Second View,nil]; I have synthesized
I have this code in a prepareForSegue method // Get destination view UIViewController *viewController
I have this code for my soundboard, public class soundboardActivity extends Activity { View
I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
I have this view (DetailsContainer, first class in code section of this question) with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.