【问题标题】:How can I use the SelectedRow object of a MVC WebGrid from within the same view?如何在同一视图中使用 MVC WebGrid 的 SelectedRow 对象?
【发布时间】:2016-07-15 18:57:49
【问题描述】:

我在页面上有一个 MVC WebGrid,我的网格上每行都有一个选择链接。在同一个视图中,我想获取与 WebGrid.SelectedRow 对象关联的模型对象并在视图中使用它。

我发现的每个访问SelectedRow 对象的示例都提供了有关如何使用该对象将对象传递到另一个视图/部分视图的说明。就我而言,我想从与 WebGrid 相同的视图中访问此对象。

这里是一些通用的示例代码,带有与错误相关的 cmets。

@{

    var grid = new WebGrid(dataCollection)
    @grid.GetHTML(columns: "ID", "User ID"), 
        grid.Column("UserName","Name"),
        grid.Column("", format: @<text>@item.GetSelectLink("Edit")</text>)
}

<!-- FURTHER DOWN IN MY MARKUP //-->

<div class="widget">
@if(grid.HasSelection)
{
    var obj = @grid.SelectedRow;

    //The example below reports the following error when 
    //navigating to this page:
    //
    //CS0039: Cannot convert type 
    //  'System.Web.Helpers.WebGridRow' to 
    //  'Models.User' via a reference 
    //  conversion, boxing conversion, unboxing conversion, 
    //  wrapping conversion, or null type conversion
    usr = obj as User;
    if (usr != null) <text>usr.ID</text>;
         <!-- FIND A WAY TO PRINT SELECTED CONTENT -->
}
</div>

我也尝试过转换语句,例如((User)@grid.SelectedRow)。在这种情况下,页面会加载,但是当我尝试选择一条记录并点击上述行时,浏览器会给我不同但相似的错误。

如何从与我的 WebGrid 相同的视图中访问和使用 WebGrid.SelectedRow 模型对象?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 webgrid


    【解决方案1】:

    希望下面的回答对你有所帮助。它将显示选定的行内容

    @{
        List<Person> person = new List<Person>();
        person.Add(new Person { PersonCode = "1001", PersonName = "Satya Nadella" });
        person.Add(new Person { PersonCode = "1002", PersonName = "Lisa Su" });
        person.Add(new Person { PersonCode = "1003", PersonName = "Jeff Clarke" });
        person.Add(new Person { PersonCode = "1004", PersonName = "Mark Fields" });
        person.Add(new Person { PersonCode = "1005", PersonName = "Phebe Novakovic" });
        person.Add(new Person { PersonCode = "1006", PersonName = "Mary T. Barra" });
        person.Add(new Person { PersonCode = "1007", PersonName = "Rajeev Suri" });
        person.Add(new Person { PersonCode = "1008", PersonName = "Michel Combes" });
    }
    
    @{
        WebGrid grid = new WebGrid(person, rowsPerPage: 10);
    
        @grid.GetHtml(columns: grid.Columns(grid.Column("PersonCode", "Code"), grid.Column("PersonName", "Name"), grid.Column("", format: @<text>@item.GetSelectLink("Edit")</text>)))
    }
    
    <div>
        @if (grid.HasSelection)
        {
            if (grid.SelectedRow != null)
            {
                <div>
                    Code: @grid.SelectedRow.Value.PersonCode
                </div> 
                <div>
                    Name: @grid.SelectedRow.Value.PersonName
                </div> 
            }        
        }
    </div>
    

    【讨论】:

    • 这行得通。我不知道 Value 属性。在代码中放置断点并检查内存中的值暗示了一些不同的东西。具体来说,SelectedRow 对象应该是我的模型对象。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多