Hi WB experts, need you help, how to add conitions inside grid.column, here is what I am trying to do, based on a condition want to display share or reply. Below is the code,
if shared = "Y" then
want to diplay "grid.Column(columnName:"Reply",format: @<a href="~/Reply?EId=@item.Eid">Reply</a>),"
You cannot put the condition in the WebGrid.GetHtml method. You have to construct the columns separately as I showed, then pass the constructed columns into the GetHtml method.
I don't understand what problem you are running into. However, in your first post, you wanted to conditionally alter the column name. You can't do that the way you are going about things. You can only change the text in the column based on the condition.
Column heading is not important, i want to direct users, based on if the post has never been shared, i want to link to the page where they will share once shared, now i want that post to have only reply. So that is why you saw he condition whuch says
@if(item.shared==null) . Hope i explained what i am trying to do. It is one field but condition directing to different pages.
srini23
Member
20 Points
39 Posts
Conditions with in grid.column
Oct 31, 2012 11:33 PM|LINK
Hi WB experts, need you help, how to add conitions inside grid.column, here is what I am trying to do, based on a condition want to display share or reply. Below is the code,
if shared = "Y" then
want to diplay "grid.Column(columnName:"Reply",format: @<a href="~/Reply?EId=@item.Eid">Reply</a>),"
elseThnaks in advance.SriniMikesdotnett...
All-Star
155647 Points
19986 Posts
Moderator
MVP
Re: Conditions with in grid.column
Nov 01, 2012 05:52 AM|LINK
You can construct the collection of columns separately from the grid:
var grid = new WebGrid(data); var columns = new List<WebGridColumn>(); columns.Add( grid.Column("Somecol", ""); if(shared == "Y"){ columns.Add( grid.Column(columnName: "Reply", format: @<a href="~/Reply?EId=@item.Eid">Reply</a>); } else { columns.Add(grid.Columns(columnName: "Share", format: @<a href="~/Share?Eid=@item.Eid">Share</a>); } ....Then
Web Pages CMS | My Site | Twitter
srini23
Member
20 Points
39 Posts
Re: Conditions with in grid.column
Nov 03, 2012 02:27 AM|LINK
Mikesdotnett...
All-Star
155647 Points
19986 Posts
Moderator
MVP
Re: Conditions with in grid.column
Nov 03, 2012 07:48 AM|LINK
You cannot put the condition in the WebGrid.GetHtml method. You have to construct the columns separately as I showed, then pass the constructed columns into the GetHtml method.
Web Pages CMS | My Site | Twitter
srini23
Member
20 Points
39 Posts
Re: Conditions with in grid.column
Nov 03, 2012 10:03 PM|LINK
Mike - I am able get this to work,can you help me to change it to hyper link and pass the parameter to the next cshtml please.
instead of <p>Share </p> i want "<a href="~/ShareEvents?EId=@item.Eid">" , what am i doing wrong, would apprecite if you can helpMikesdotnett...
All-Star
155647 Points
19986 Posts
Moderator
MVP
Re: Conditions with in grid.column
Nov 03, 2012 10:58 PM|LINK
I don't understand what problem you are running into. However, in your first post, you wanted to conditionally alter the column name. You can't do that the way you are going about things. You can only change the text in the column based on the condition.
Web Pages CMS | My Site | Twitter
srini23
Member
20 Points
39 Posts
Re: Conditions with in grid.column
Nov 03, 2012 11:10 PM|LINK
Column heading is not important, i want to direct users, based on if the post has never been shared, i want to link to the page where they will share once shared, now i want that post to have only reply. So that is why you saw he condition whuch says
Mikesdotnett...
All-Star
155647 Points
19986 Posts
Moderator
MVP
Re: Conditions with in grid.column
Nov 04, 2012 07:45 AM|LINK
grid.Column("", "", format: @<text>@(item.share == "Y" ? Html.Raw("<a href="~/ShareEvents? EId=@item.Eid">Share</a>") : Html.Raw("<a href="~/RepliedEvents?EId=@item.Eid">Reply</a>")) </text>)Web Pages CMS | My Site | Twitter
GmGregori
Contributor
5564 Points
749 Posts
Re: Conditions with in grid.column
Nov 05, 2012 12:22 PM|LINK
If you prefer to use the if-else statement try
grid.Column(format: @<text> @if (item.shared == null){ @Html.Raw("<a href='ShareEvents?EId=" + @item.Eid +"'>Share</a>") } else { <p>Reply</p> } </text> )srini23
Member
20 Points
39 Posts
Re: Conditions with in grid.column
Nov 07, 2012 03:21 AM|LINK
Thanks GmGregori and Mike,
You reply fixed the issue and thanks for the code. :=)
Srini