Eray Balkanli Eray Balkanli - 1 year ago 59
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 mvc4.

columnBuilder.For(o => o.SpecificProcedure)
.Attributes(style => "text-align:center")

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.


Answer Source

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"

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download