Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Nesting in ternary operator in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 73
    Comment on it

    While writing codes in your MVC code for fetching data or for any purpose you need to use the condition based operation several times.

     

     

    One way is to use the if else condition for it which includes too many lines in your code. Other way is to use ternary operator to avoid the too much code. If you want to check condition inside a condition nesting is performed in if else condition.
     

    public ProductResponse GetProduct()
            {
                List<ProductModel> lstModel = new List<ProductModel>();
                ProductResponse ProductResponse = new ProductResponse();
                try
                {
                    SqlDataReader reader = null;
                    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                    {
                        if (sqlConnection.State == ConnectionState.Closed)
                        {
                            sqlConnection.Open();
                        }
                        reader = SqlHelper.ExecuteReader(sqlConnection, CommandType.StoredProcedure, "uspGetNudgeLocations");
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                lstModel.Add(new ProductModel
                                {
                                    Id = reader["ProductId"] != null ? !string.IsNullOrEmpty(Convert.ToString(reader["ProductId"])) ? Convert.ToInt32(Convert.ToString(reader["ProductId"]).Trim()) : 0 : 0,
                                    ProductName = reader["ProductName"] != null ? Convert.ToString(reader["ProductName"]).Trim() : string.Empty
                                });
                            }
                        }
    
                        else
                        {
                            lstModel.Add(new ProductModel
                            {
                                ProductName = "No Product Found"
                            });
                        }
                    }
    
                    ProductResponse.ProductList = lstModel;
                    ProductResponse.Status = true;
                }
    
                catch (Exception ex)
                {
                    ProductResponse.Message = ex.Message;
    
                    ProductResponse.Status = false;
                }
                return ProductResponse;
            }
    

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: