如何将查询结果发送到View?(MVC 4& Azure Table Storage)

时间:2013-05-27 19:12:30

标签: asp.net-mvc azure

我是新来的,所以请耐心等待。我正在构建一个连接到现有azure表存储的MVC 4应用程序。现在我需要创建一些“过滤器”,这意味着我必须做一些查询并将结果显示给应用程序的一个页面。 现在我有以下内容:

型号:

public class psEntity : TableEntity
    {
        public psEntity() { }
        public string Message { get; set; }
        public string RoleName { get; set; }
        public string BatchName { get; set; }
        public string DeploymentID { get; set; }
        public string Description { get; set; }
    }

查看

 @model IEnumerable<MvcApplication6.Models.psEntity>

@{
    ViewBag.Title = "Performance Status";
}

<h2>Performance Status</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Message)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RoleName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Message)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RoleName)
        </td>

    </tr>
}

控制器:

public class MailingListController : Controller
    {
        public MailingListController()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
        }

        public ActionResult Index()
        {   
            TableQuery<psEntity> query=new TableQuery<psEntity>().Where(
                TableQuery.GenerateFilterCondition("RoleName", 
                QueryComparisons.Equal,"Contacts.Worker.Azure"));
    foreach (psEntity list in query)
                {
                    lists.Add(list);
                }
                return View(lists);

        }

我的问题是查询后出现的问题,以便仅显示我在视图中创建的表格中的特定数据。

我尝试使用foreach但是出错了,

  

“foreach语句不能对类型的变量进行操作   Microsoft.WindowsAzure.Storage.Table.TableQuery&GT; MVCApplication6.Models.psEntity&GT;   因为它不包含'GetEnumerator'的公共定义。“

提前谢谢

1 个答案:

答案 0 :(得分:0)

检查一下:

Retrieve all entitiesHow to: Retrieve all entities in a partition

要查询分区中所有实体的表,请使用TableQuery对象。以下代码示例为“Smith”是分区键的实体指定过滤器。此示例将查询结果中每个实体的字段打印到控制台。

// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Create the CloudTable object that represents the "people" table.
CloudTable table = tableClient.GetTableReference("people");

// Construct the query operation for all customer entities where PartitionKey="Smith".
TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Smith"));

// Print the fields for each customer.
foreach (CustomerEntity entity in table.ExecuteQuery(query))
{
    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
        entity.Email, entity.PhoneNumber);
}