Eray Balkanli Eray Balkanli - 1 month ago 5
ASP.NET (C#) Question

How to place if-else statement affecting a column's value in aspx mvc4

I have a code like below in asp.net mvc4.

...
Html.Grid(..).Columns.ColumnBuilder=>
...
columnBuilder.For(o => o.SpecificProcedure)
.Attributes(style => "text-align:center")
.SortColumnName("SpecificProcedure")
.Named("SpecificProcedure");
...


What I want is to show "Approved" or "Not Approved" based on o.SpecificProcedure value. I need to place an if-else statement like
(o => if(o.SpecificProcedure == 1) print Approved, else print Not Approved)
. I could not find the correct syntax for it. I appreciate if I get some help.

Regards

Answer

Are you using a ViewModel to bind to your View? If so, just put all that logic in the ViewModel and bind like you normally would. The ViewModel should reflect the data you want to show on the page, this may be information that is passed through from a database query, but it also gives you the opportunity to decouple your db entities from your UI.

public class SomeViewModel
{
    public string SpecificProcedureDisplayValue { get; set; }
}

then populate it like

        var viewModel = dbContext.entities
            .Where(x => x.SomeProperty == "some value")
            .Select(x => new SomeViewModel
            {
                SpecificProcedureDisplayValue = x.SpecificProcedure == 1 ? "Approved" : "Not Approved"
            }).ToList();

I'm making the assumption you are using EF to grab data from a database.

Comments