3

I am having a problem displaying different text based on the result I get from my controller.

Column command_status_codeis returning value between 0 to 12 from the table. However, I would like to display different text based on the values I get from the controller.

i.e if I get 0 I would like to display Verify and if I get 1 I would like to display Active and so on.

I am not sure if I add the check in the view or do the conversion in the controller itself.

Here is the relevant code:

View

@model List<Models.AuditLogs>

<table>
  <tr>
    <th>User</th>
    <th>Command Status Code</th>
  </tr>
  @foreach (var AuditLogsDetail in Model)
  {
  <tr>
    <td>@AuditLogsDetail.user_id</td>
    <td>@AuditLogsDetail.command_status_code</td>
  </tr>
  }
</table>

Controller

public ActionResult AuditLogs() {
    string connectionstring = "MY_CONNECTION_STRING";
    string sql = "select * from table_name";
    SqlConnection conn = new SqlConnection(connectionstring);
    SqlCommand cmd = new SqlCommand(sql, conn);
    var Details = new List < AuditLogs > (); {
        conn.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read()) {
            var AuditLogsDetail = new AuditLogs {
                user_id = rdr["user_id"].ToString(),
                command_status_code = rdr["command_status_code"].ToString(),
            };
            Details.Add(AuditLogsDetail);
        }
    }
    return View(Details);
}

Model

public class AuditLogs
    {
        public string user_id { get; set; }
        public string command_status_code { get; set; }
    }
}
3
  • Why aren't you using an Enum instead? Commented Dec 21, 2017 at 15:21
  • @CamiloTerevinto I do not have access to change the table structure and thus I am working with string instead Commented Dec 21, 2017 at 15:32
  • I'd suggest revisiting the enum idea mentioned by @Camilo Terevinto. You don't need to change the table structure. Add an enum with the int mappings you want, then in your model just cast your code to the enum. It's less verbose than a switch. Commented Dec 21, 2017 at 15:54

3 Answers 3

4

I would leave the Controller for routing or controlling which view is called (which should be it's job, you should put as few presentation or application logic in the controller as possible).

Since this conversion is logic related to the model itself, I would put it in the Model class where it belongs and can be tested easily (if it becomes more complicated).

I would add a new property in the AuditLogsDetail model class which would return a string using a switch statement (since there are many possible values):

public class AuditLogsDetail 
    {
        public int CommandStatusCode { get; set; }

        public string CommandStatus
        {
            get
            {
                switch (CommandStatusCode)
                {
                    case 0:
                        return "Verify";

                    case 1:
                        return "Active";

                    // and so on for the other 12 cases                        

                    default:
                        // you could throw an exception here or return a specific string like "unknown"
                        throw new Exception("Invalid Command Status Code");
                }
            }
        }
    }

In the Razor view you then simply have to call this property likewise:

<tr>
  <td>@AuditLogsDetail.user_id</td>
  <td>@AuditLogsDetail.CommandStatus</td>
</tr>

You could put a switch statement or if statement in the view but then you would clutter it. If you have several of these statements the view would be hard to read.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you explain what is [DataMember]
@zaq I'll remove it. It's for serialization. When you model class is sent across the network. Which happens in the architecture I work with but it might not be required in your case. Force of habit.
@zaq CommandStatusCode is your command_status_code, it's just that in C# the convention if to write property names PascalCase. But you can write them however you want.
1

This sounds like a good candidate for an enum.

enum CommandStatus
{
    def = 0,
    success = 1,
    A = 2,
    B = 3,
    ...
}

public class AuditLogs
{
    public string user_id { get; set; }
    public CommandStatus command_status_code { get; set; }
}

Then when you fetch the value, just cast the int to the enum:

var AuditLogsDetail = new AuditLogs
                {
                    user_id = rdr["user_id"].ToString(),
                    command_status_code = (CommandStatus)rdr["command_status_code"],
                };

This doesn't provide the robustness of a switch, however.

Comments

-2

in your view you can do something like this:

@model List<Models.AuditLogs>

<table>
  <tr>
    <th>User</th>
    <th>Command Status Code</th>
  </tr>
  @foreach (var AuditLogsDetail in Model)
  {
  @if (@AuditLogsDetail.command_status_code == 0)
  {
    // what i need to do
  }
  @else if (@AuditLogsDetail.command_status_code == 1)
  {
    // what i need to do
  }
  <tr>
    <td>@AuditLogsDetail.user_id</td>
    <td>@AuditLogsDetail.command_status_code</td>
  </tr>
  }
</table>

3 Comments

What is myVarToTest ? Also, why should I still display <td>@AuditLogsDetail.command_status_code</td> in the code.
Oh my bad, replace myVarToTest by @AuditLogsDetail.command_status_code and it will check what you want :)
Could you edit your answer to reflect the changes? It would not be of much help for anyone to see the incomplete snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.