ASP.NET MVC3 webgrid conditional statement on column (if, if else loop)

How to conditionally format a grid cell?
asp.net mvc 3 – Custom message inside the webgrid
asp.net mvc 3 – Mvc3 webgrid conditional statement on column
This article will show you how to use if condition in ASP.NET MVC Webgrid to manage the column text.
In my grid I have a column “Status” bool field, if Status is true, It will show “Active” else “Inactive“.
Something like RowDataBound in web form.

grid.Column(
    header: "Status",
    format: (item) =>
        (bool)item.Status  == true
            ? Html.Raw("Active")
            : Html.Raw("Inactive")
)

On the same way we can add links in if condition.

grid.Column(
    header: "Detail",
    format: (item) =>
        (bool)item.IsActive
            ? Html.ActionLink(
                    (string)item.FullName,
                    "Detail",
                    new
                    {
                        id = item.UserId
                    },
                    new
                    {
                        style = "color: 'red'",
                        onmouseover = "this.style.color='green'",
                        onmouseout = "this.style.color='black'"
                    }
                )
            : Html.Raw("Inactive")
)

In the same way we can add if else condition to manage column content .

grid.Column(
    header :"Description",
    format: (item) =>
        {
            if (item.IsActive)
                return Html.ActionLink("Delete", "Delete", new { id = item.id });
            else
                return Html.ActionLink("Edit", "Edit", new { id = item.id });
        }
)

On the same way we can add Edit, Delete, Details as below

grid.Column(columnName:"ID", header:"Actions",      
format: (item) => 
{
    var links = Html.ActionLink("Edit", "Edit", new {id = item.ID}) + " | " +
                Html.ActionLink("Details","Details", new { id = item.ID}) +" | "+
                Html.ActionLink("Delete","Delete", new { id = item.ID});
    return Html.Raw(links);
})

Relevant links
Sorting in webgrid in ASP.NET MVC
Delete Multiple webgrid row using jQuery
Efficient Paging and Sorting with WebGrid Web Helper – ASP.NET MVC
ASP.NET MVC3 webgrid conditional statement on column (if, if else loop)
Efficient Paging with WebGrid Web Helper – ASP.NET MVC
ASP.NET MVC Web Grid Style

Advertisements
This entry was posted in ASP.NET MVC Web Grid, MVC and tagged , . Bookmark the permalink.

One Response to ASP.NET MVC3 webgrid conditional statement on column (if, if else loop)

  1. imi khan says:

    your this article help me. thank you so much

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s