4

I passed List as Model to WebGrid Constructor.

@model List<string>

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

<div> @grid.GetHtml() </div>

I am expecting to get my List of string with grid. But I got length of string. Weird?

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
ttacompu
  • 873
  • 1
  • 10
  • 15

2 Answers2

2

That is weird, but the explanation is that the WebGrid looks for public properties of the row objects in order to display them as columns; it just so happens that Length is the only property that type String exposes.

Note that you can get your desired behavior by specifying the format explicitly:

@grid.GetHtml(
    columns: new[] {
        g.Column(format: (item) => item)
    }
)
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
0
@{
Boolean access = ViewBag.accesscontrols;   
}
<h1 class="header_text_pink">Vendor Master List<img    
src='@Url.Image("~/content/images/question_mark.png")' alt="" class="information_icon"   
/></h1>
<span>@Html.RenderNotificationMessage()</span>
<div class="si_top_main_table" id="tableDiv_Arrays">
<div class="search_pink">
<div  class="tablesorter_pink" id="targetContainer">
@{
List<FCC.POSS.Common.Models.Employee> listmodel = ViewBag.EmployeeList;
ViewBag.Title = "Employee Master";
} 
@if (listmodel != null)
{
if (listmodel.Count > 0)
{
List<WebGridColumn> gridColumn = new List<WebGridColumn>();
var grid = new WebGrid(source: listmodel,
rowsPerPage: listmodel.Count(),
canSort: true,
canPage: true);
gridColumn.Add(grid.Column("Fname", header: "First Name"));
gridColumn.Add(grid.Column("Lname", header: "Last Name"));
gridColumn.Add(grid.Column("Address", header: "Address"));
gridColumn.Add(grid.Column("City", header: "City"));
gridColumn.Add(grid.Column("Phoneno", header: "Phone no"));
gridColumn.Add(grid.Column("Passportno", header: "Passport no"));
gridColumn.Add(grid.Column("Civilid", header: "Civil Id"));
gridColumn.Add(grid.Column("Gender", header: "Gender"));
gridColumn.Add(grid.Column("Createddt", header: "Created Date"));
gridColumn.Add(grid.Column("Deleteddt", header: "Deleted Date"));
gridColumn.Add(grid.Column("Isdelete", header: "Is Deleted ?"));
// gridColumn.Add(grid.Column("View", format:@<text><span style=" background-
color:Blue;  
color:Green">@Html.ModalDialog("View","ag",null,Url.Action("Details","Employee",new 
{item.Id}),false)</span></text>));
@grid.GetHtml(
tableStyle:"display tablesorter_usermaster",
columns:grid.Columns(gridColumn.ToArray()),
htmlAttributes: new { id = "mst_WebGrid", @style = "width:100%;" });
}
} 
</div>
</div>
</div>
<h2>Index</h2>
Shahnawaz
  • 101
  • 1
  • 1
  • 4