首页 > 解决方案 > 列出 ASP.NET MVC 中的资源记录

问题描述

我正在尝试从表中的记录集中列出资源记录。我可以显示所有资源记录,但我需要的是它只显示传递给 ActionResult 的记录集的资源记录。

澄清一下,我有一个 RecordSet 视图,其中包含托管区域中所有记录集的表,当有人单击它旁边的字段时,它会获取该记录集的名称并将其作为值(字符串答案)传递给ResourceRecords 控制器。然后它使用它来设置StartRecordName. 之后我MaxItems设置为 1。所以这基本上是搜索功能。这有效,除非有多个资源记录集命名为相同的东西,它不会列出它们的记录。它只取第一个资源记录集记录。

这是我在控制器中的内容:

public ActionResult ResourceRecords(string answer)
{
            //create Route 53 Client using the credentials
            IAmazonRoute53 route53Client = new AmazonRoute53Client("AccessKey", "SecretKey", RegionEndpoint.USEast1);

            // This gives us an object of this type: https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Route53/TListResourceRecordSetsResponse.html
            var response = route53Client.ListResourceRecordSets(new ListResourceRecordSetsRequest
            {
                HostedZoneId = "HostedZoneId",
                StartRecordName = answer,//start with the record set from the value passed in ResourceRecordSets.cshtml
                
                MaxItems = "1"
            });

            //List for RecordSets and Resource Records
            List<String> recordSetQuery = new List<String>();
            List<String> resourceRecordQuery = new List<String>();

            foreach (var recordset in response.ResourceRecordSets)
            {
                //add the record set name to the List
                recordSetQuery.Add(recordset.Name);

                foreach (var resourceRecord in recordset.ResourceRecords)
                {
                    if (ModelState.IsValid && !String.IsNullOrWhiteSpace(answer))
                        if (recordSetQuery.Contains(answer))//find the value in the query(the resource record set List)
                        {
                            resourceRecordQuery.Add(resourceRecord.Value);                       
                        }
                }
            }

            ViewBag.Test = resourceRecordQuery;
        }

        return View();
}

这是视图中的内容:

<table class="table table-striped table-bordered table-hover table-condensed">
    <td colspan="2">Resource Record</td>
    @foreach (var Records in ViewBag.Test)
    {
        <tr>
            <td>
                @Records
            </td>
            <td>
                <button>Edit</button>
            </td>
        </tr>
    }
</table>

这是我第一次这样做,所以我还在学习,我可能会让它变得更难,所以任何帮助都会很好。如果我没有发布所有需要的东西,请告诉我,我会发布它。我不想用不需要的代码淹没帖子。

标签: c#asp.net-mvc

解决方案


推荐阅读