Home > DeveloperSection > Forums > ASPX View MVC3 WebGrid format column
Mark Devid
Mark Devid

Total Post:101

Points:707
Posted on    November-20-2014 2:50 AM

 ASP.Net ASP.NET MVC  View  Grid  Web 
Ratings:


 1 Reply(s)
 1285  View(s)
Rate this:

<%{

     WebGrid studentGrid = new WebGrid(rowsPerPage: StudentController.PageSize);

     studentGrid.Bind(Model.Students, autoSortAndPage: false, rowCount: Model.RowCount);

     studentGrid.PageIndex = Model.CurrentPage;

}%>

<%=studentGrid.GetHtml(columns: new WebGridColumn[]{

    studentGrid.Column("StudentId", "Id"),

    studentGrid.Column("Name", "Name"),

 })%>

Unfortunatly i am forced to use aspx view in my MVC3 project.

I want to have a column that shows a text "select" or "remove" based on some condition of the list item that is bound to the grid.

How is the sysntax to do that

I have to get render html like

<span class="1" id=item.id>Select<span>

and the html shown will be just Select



jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    November-20-2014 6:10 AM

You can definitely do it with format you just need to handcraft the HTML in C#:

<%

 

    var list = new[]

                    {

                        new { StudentId = 1, Name = "Name1", Cond = true },

                        new { StudentId = 2, Name = "Name3", Cond = false },

                        new { StudentId = 2, Name = "Name3", Cond = true },

                    };

    WebGrid studentGrid = new WebGrid();

    studentGrid.Bind(list, autoSortAndPage: false, rowCount: 3);

 

%>

<%= studentGrid.GetHtml(columns:

    new WebGridColumn[]

        {

            studentGrid.Column("StudentId", "Id"),

            studentGrid.Column("Name", "Name"),

            studentGrid.Column(header: "Action", 

                format: item =>

                {

                    string span = "<span class=\"1\" id=\"{0}\">{1}<span>";

                    string action = item.Cond ? "Select" : "Remove";

                    return Html.Raw(string.Format(span, item.StudentId, action));

                })

        })

%>

Using the aspx template systax (eg. <% %>) inside a lambda is supported in general (see this Telerik demo) but because the WebGrid working diffidently than Telrik it's not working.

It seems the way is WebGrid built only supports razor templates inside the format argument


Don't want to miss updates? Please click the below button!

Follow MindStick