Reputation: 912
I can't get the data to display on the razor view. I am currently getting the data and filling my object in the DataAccessLayer.GetData() function. My objects list gets filled and stored. However my objects list count is 0 on the razor view therefore it shows nothing. Am I "newing" up my Object correctly on the view? This is the first time using the razor pages template so it's a learning curve.
Razor Page:
<form method="post" asp-page-handler="GetData">
<input id="txtSearch" name="search" />
<button class="btn btn-lg btn-dark" id="btnSubmit" name="Submit" />
</form>
@{ List<ObjectsModel> objects = new List<ObjectsModel>();
@foreach (var record in objects)
{
<tr>
<td>
@Html.DisplayFor(modelItem => record.COLUMN1)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN2)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN3)
</td>
</tr>
}
}
PageModel:
public class ObjectsModel : PageModel
{
public string COLUMN1 { get; set; }
public string COLUMN2 { get; set; }
public string COLUMN3 { get; set; }
public void OnGet()
{
}
public void OnPostGetData(string search)
{
List<ObjectsModel> objects = new List<ObjectsModel>();
DataAccessLayer.GetData(search, out objects);
return;
}
}
DataAccessLayer:
public class DataAccessLayer
{
public static bool GetData(string ID, out List<ObjectsModel> objects)
{
string connectionString = "";
objects = new List<ObjectsModel>();
try
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = @"SELECT top 50 COLUMN1, COLUMN2, COLUMN3
"FROM Table";
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
ObjectsModel item = new ObjectsModel();
item.COLUMN1 = reader.GetValue(0).ToString().Trim();
item.COLUMN2 = reader.GetValue(1).ToString().Trim();
item.COLUMN3 = reader.GetValue(2).ToString().Trim();
objects.Add(item);
}
reader.Close();
reader = null;
command = null;
connection.Close();
connection = null;
return true;
}
catch (Exception exc)
{
exc.ToString();
return false;
}
}
}
Upvotes: 3
Views: 17973
Reputation: 912
I got it working by adding my objects List property into my Objects Model and initializing it there rather than on the view page. I no longer need a reference in the Razor View as I can access the list in the my loop with @Model.objects. Obviously I have changed the name of my page model and properties since this will be a live project in production eventually.
Updated PageModel:
public class ObjectsModel : PageModel
{
public class Object
{
public string COLUMN1 { get; set; }
public string COLUMN2 { get; set; }
public string COLUMN3 { get; set; }
public List<ObjectsModel> objects = new <ObjectsModel>();
}
public void OnGet()
{
}
public void OnPostGetData(string search)
{
DataAccessLayer.GetData(search, out objects);
return;
}
}
Updated Razor Page:
@{
@foreach (var record in @Model.objects)
{
<tr>
<td>
@Html.DisplayFor(modelItem => record.COLUMN1)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN2)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN3)
</td>
</tr>
}
}
Upvotes: 2
Reputation: 395
Change this section of code
@{ List<ObjectsModel> objects = new List<ObjectsModel>();
@foreach (var record in objects)
{
<tr>
<td>
@Html.DisplayFor(modelItem => record.COLUMN1)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN2)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN3)
</td>
</tr>
}
}
to this
public class ObjectsModel : PageModel
{
public string COLUMN1 { get; set; }
public string COLUMN2 { get; set; }
public string COLUMN3 { get; set; }
public List<ObjectsModel> objects{get; set;}
public void OnGet()
{
}
public void OnPostGetData(string search)
{
DataAccessLayer.GetData(search, out objects);
return;
}
}
@{
@foreach (var record in Model.objects)
{
<tr>
<td>
@Html.DisplayFor(modelItem => record.COLUMN1)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN2)
</td>
<td>
@Html.DisplayFor(modelItem => record.COLUMN3)
</td>
</tr>
}
}
Upvotes: 2