asp.net-mvc 如何在MVC WebGrid中显示行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12034460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:11:02  来源:igfitidea点击:

How to display row number in MVC WebGrid

asp.net-mvcasp.net-mvc-3razor

提问by M.Azad

I want to have a column as row numberin MVC WebGrid. How can I do it?

我想row number在 MVC WebGrid 中有一列。我该怎么做?

回答by takemyoxygen

That's a really nice approach, but when you use sorting or paging your RowNumbervalues won't start from 1 on the page.

这是一个非常好的方法,但是当您使用排序或分页时,您的RowNumber值不会从页面上的 1 开始。

In my project I had a case where I needed to know an index of the row independently of WebGrid's paging / sorting and I came across the following solution:

在我的项目中,我有一个案例,我需要独立于 WebGrid 的分页/排序了解行的索引,我遇到了以下解决方案:

grid.Column(
    Header: "RowNumber",
    Format: item => item.WebGrid.Rows.IndexOf(item) + 1
)

回答by Darin Dimitrov

You could use a view model that will contain a property indicating the row number.

您可以使用包含指示行号的属性的视图模型。

Let's suppose that you have the following domain model:

假设您有以下域模型:

public class DomainModel
{
    public string Foo { get; set; }
}

Now you build a view model that will correspond to the requirements of your view:

现在您构建一个视图模型,该模型将对应于您的视图的要求:

public class MyViewModel
{
    public int RowNumber { get; set; }
    public string Foo { get; set; }
}

and then:

进而:

public ActionResult Index()
{
    // fetch the domain model from somewhere
    var domain = Enumerable.Range(1, 5).Select(x => new DomainModel
    {
        Foo = "foo " + x
    });

    // now build the view model
    // TODO: use AutoMapper to perform this mapping
    var model = domain.Select((element, index) => new MyViewModel
    {
        RowNumber = index + 1,
        Foo = element.Foo
    });

    return View(model);
}

Now your view becomes strongly typed to the view model of course:

现在,您的视图当然变成了视图模型的强类型:

@model IEnumerable<MyViewModel>

@{
    var grid = new WebGrid(Model);
}

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column("RowNumber"),
        grid.Column("Foo")
    )
)

Now let's suppose that for some foolish reason you don't want to use view models. In this case you could turn your view into spaghetti code if you prefer:

现在让我们假设出于某种愚蠢的原因您不想使用视图模型。在这种情况下,如果您愿意,可以将视图转换为意大利面条式代码:

@model IEnumerable<DomainModel>

@{
    var grid = new WebGrid(Model.Select((element, index) => new { element, index }));
}

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column("RowNumber", format: item => item.index + 1),
        grid.Column("Foo", format: item => item.element.Foo)
    )
)

回答by Tinu Mathew

simply add the following code

只需添加以下代码

grid.Column(header: "No."
,format: item => item.WebGrid.Rows.IndexOf(item) + 1 
          + Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) 
            * grid.RowsPerPage * grid.PageIndex)

Check this linkfor more info

检查此链接以获取更多信息

hope this will be helpful to someone

希望这对某人有帮助

回答by Jameel

    @{
    int i=0;
foreach (var item in Model) {
    <tr>
    <td>
    @i
    </td>
        <td>
            @Html.DisplayFor(modelItem => item.Expense)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { onclick = "return confirm('Are you sure you wish to delete this record?');" })

        </td>
    </tr>
    i++;
}
}

Try this

尝试这个

回答by Snehlata Shaw

Add this:

添加这个:

grid.Column(header: "No.",
        format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) * grid.RowsPerPage * grid.PageIndex)