剃刀页面-Foreach遍历列表以获取要显示在剃刀视图中的数据

时间:2019-11-22 16:33:48

标签: c# asp.net-core razor-pages asp.net-core-3.0

我无法在剃刀视图上显示数据。我目前正在获取数据,并在 DataAccessLayer.GetData()函数中填充对象。我的对象列表被填充并存储。但是,我的对象列表在剃刀视图中的计数为0,因此什么也不显示。我是否在视图上正确地“更新” 我的对象?这是第一次使用剃须刀页面模板,因此这是一个学习曲线。

剃刀页面:

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

2 个答案:

答案 0 :(得分:1)

更改此部分代码

@{ 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>
    }
}

对此

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

答案 1 :(得分:0)

我通过将对象的List属性添加到我的Objects Model中并在此处而不是在视图页面上对其进行初始化来使其工作。我不再需要Razor View中的引用,因为我可以使用@ Model.objects在my循环中访问列表。显然,我已经更改了页面模型和属性的名称,因为这最终将成为生产中的实时项目。

更新后的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;
    }
}

更新的剃刀页面:

@{

    @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>
    }
}