简体   繁体   中英

Using if statement in MVC view and controller

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

Column command_status_code is 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.

ie 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; }
    }
}

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM