Node is saved as draft in My Content >> Draft
Nesting in ternary operator in MVC
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;
}
0 Comment(s)