Vertical table header in ASP.NET MVC 5

Can anyone recommend how to reposition Headers from the top to the left column vertically? I would also like to know if there are any jQuery plugins available for directly editing tables from a grid with examples :) Since I am new to this...

<table class="table table-striped table-hover">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Age)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Country)
            </td>

            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>

https://i.sstatic.net/mhWYL.png

Answer №1

Give this a shot...

<table class="table table-striped table-hover">
    <tr>
        <td>
            @Html.DisplayNameFor(model => model.Name)
        </td>
        @foreach (var item in Model)
        {
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
        }
    </tr>
    <tr>
        <td>
            @Html.DisplayNameFor(model => model.Age)
        </td>
        @foreach (var item in Model)
        {
            <td>
                @Html.DisplayFor(modelItem => item.Age)
            </td>
        }
    </tr>
    <tr>
        <td>
            @Html.DisplayNameFor(model => model.Country)
        </td>
        @foreach (var item in Model)
        {
            <td>
                @Html.DisplayFor(modelItem => item.Country)
            </td>
        }
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
        @foreach (var item in Model)
        {
            <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id })
            @Html.ActionLink("Details", "Details", new { id = item.Id })
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        }
    </tr>
</table>

Answer №2

If you want to display the data in a table horizontally, you need to ensure that the data is selected in a list format. To achieve this, you will have to adjust the structure of your ViewModel as follows:

public class TestViewModel
{
    public List<string> Name {get; set;}
    public List<int> Age {get; set;}
    public List<string> Country {get; set;}
}

For displaying the data in the View using a table:

@model ProjectName.Models.ViewModels.TestViewModel

<table class="table table-striped table-hover">

    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        @foreach (var item in Model.Name)
        {
         @Html.DisplayFor(modelItem => item.Name)
        }
    </tr>

    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        @foreach (var item in Model.Age)
        {
            @Html.DisplayFor(modelItem => item.Age)
        }
    </tr>

    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        @foreach (var item in Model.Country)
        {
            @Html.DisplayFor(modelItem => item.Country)
        }
    </tr>
</table>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to achieve impersonation all the way from an ASP.NET website to a WCF service?

Let me explain my situation briefly: The user initiates an action in the web browser, triggering an Ajax call to a web service on one server (server A). This web server then calls a WCF service hosted in IIS on another server (server B). The WCF servi ...

Form-check radio buttons within the form-check-inline class of Bootstrap 4.1.1

I am attempting to showcase a radio button as an inline option. According to the Bootstrap 4.1.1 documentation, the example code is: <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="inlineRadioOption ...

How come my blocks are spilling over into other blocks?

I have successfully created a webpage using Drupal 8 and Bootstrap, featuring a "Learn more" button to expand the details. However, I am facing an issue where the information blocks overflow when collapsed. The gray line and label are spilling into the up ...

The sorting order is not preserved by the asmSelect jQuery plugin

Currently, I am utilizing the asmSelect plugin to generate lists and also attempting to use it for editing existing lists. The asmSelect plugin provides the ability to manually arrange the selected options before submission. The issue I am facing is that ...

Incorporate external visuals into printed materials

When a page is printed, a heading should be accompanied by an image to the right. I have defined the following stylesheet: @media print { h1::after { content: url( IMAGE_URL_HERE ); position: absolute; top: 0; right: 0; } } The issue ...

Unable to add items to the global JavaScript array variable

My goal is to populate a global array variable within my ready function, but when I attempt to access the data later on, the array appears to be empty. This is how my ready function looks: var counter = 0; var services = []; var names = [] va ...

Creating a static menu icon and implementing a wide dropdown menu in Bootstrap 4

I am facing two major challenges and I am struggling to find solutions for them! Below is the code snippet I am working with: @font-face { font-family: 'yekani'; src: url("../fonts/yekan.ttf"); } @font-face { font-family: 'ye ...

No reply received from the RESTful web service triggered by jQuery using JSON

While I know there are numerous posts related to this topic, I'm struggling to make it work due to my limited experience with REST and JQuery: Currently, I am using REST-WS with Java 5 and have successfully called it using the "Poster" Firefox plugin ...

Utilizing JQuery to extract the image title and render it as HTML code

I am currently facing an issue with displaying an image in my project. I want to hide the image using CSS (display: none) and instead, retrieve the value of its title attribute and display it within the parent div by utilizing JQuery. Despite knowing tha ...

Unable to horizontally center ul element within Materialize navigation content

Struggling to center nav-content by using the center option Hoping for it to have this appearance. Unfortunately, applying it directly to the ul attribute doesn't yield desired results. Instead, it ends up looking like this. This is the code snipp ...

Having trouble with the jQuery select2 AJAX functionality?

I've been experimenting with the jQuery select2 plugin and attempting to integrate AJAX with my external data, but unfortunately it's not working as expected. I'm curious if anyone can help me identify what mistake I might be making or if th ...

Click event dynamically bound to list items

I have a web application built with Durandal and Knockout. Here is the HTML code snippet: <ul id="header"> </ul> In a JavaScript function, I am dynamically adding a list item as follows: $("#header).append('<li id="btn"><a hre ...

Tips for utilizing javascript to reset the heightline of the preceding keyword during a keyword search

Using JavaScript, I have implemented a search keyword function that highlights specific words in a paragraph. However, I am facing an issue. Currently, when searching for the next keyword, the previously highlighted text remains in red instead of reverting ...

Tips for implementing Validator controls with controls in addition to TextBox?

Is there a method to utilize the Validator control in order to validate a FCKEditor rich text control on both client and server sides? Additionally, can the Validator controls be employed for validating elements beyond text boxes? ...

Creating a tournament bracket in HTML using a table structure

For the past 3 weeks, I've been grappling with a problem that has me stumped. I just can't seem to figure it out for the life of me. What I'm attempting to achieve is a specific output or presentation using tables. Here's an example of ...

Challenges with handling Ajax responses in Ruby on Rails

I'm currently facing an issue with the Ajax response in my Rails application, and I'm unsure of how to troubleshoot it. Here is the code that is functioning correctly: View <div id="<%= dom_id(comment) %>_count"> <%= Like.wh ...

Discovering if a div element has children using Selenium IDE

As I begin to automate testing for our website with Selenium IDE, I must admit that I am still learning the ins and outs of it. We utilize highcharts to exhibit data on our site. The challenge arises from the fact that there are 3 div elements handling th ...

Creating a reusable component in Angular with a shared selector

Is it possible to create reusable HTML elements within a component and then call them separately in another component? If so, what would be the best solution to achieve this? Please share your guidance. The code I have written: I am looking to individual ...

Interactive canvas feature in a browser window with scrolling capabilities (drag and drop functionality)

One challenge I'm facing is when drawing in a canvas on a browser window that has a vertical scrollbar. The figures are in the correct position, and it's possible to interact with them by grabbing and making connections. However, this interactio ...

Creating JavaScript Objects from HTML Form data with the help of serializeJSON

Struggling extracting HTML form data into the required JSON format for server communication. Despite following a guide, I can't get the output right. Managed to extract question keys and values, but labeling is tricky. Current Output: {"Question1":" ...