One way to do it is have a class in your Models that represents a row of the database and have a method to read a row. Then you can have a Get method that returns a list of objects... something like...
public class MyItem
{
public string Id { get; set; }
public string MyProperty { get; set; }
public List<MyItem> Get()
{
List<MyItem> items = new List<MyItem>();
try
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand select = new SqlCommand())
{
select.CommandText = @"select bla bla bla from bla whete MyProperty=@MyProperty";
select.Parameters.AddWithValue("@MyProperty", this.MyProperty);
select.Connection = connection;
connection.Open();
using (SqlDataReader reader = select.ExecuteReader())
{
DataTable dataTable = new DataTable("ResultSet");
dataTable.Load(reader);
if ((dataTable != null) && (dataTable.Rows != null) && (dataTable.Rows.Count > 0))
{
foreach (DataRow row in dataTable.Rows)
{
MyItem item = new MyItem();
item.FromRow(row);
items.Add();
}
}
}
}
}
}
catch (Exception)
{
log here
throw;
}
return items;
}
public void FromRow(DataRow row)
{
Id = row["Id"].ToString();
...
}
}
In your controller you can then do something like...
[Authorize(Roles = "...")]
public IActionResult GetMyItems(string id)
{
MyItem item = new MyItem() { Id = id };
var items = item.Get();
return items;
}
Then you should be able to access using the Model property in your view.