I have a variable result
which contains a JSON object:
result = JsonConvert.DeserializeObject<PullRequestModel>(responseData);
I would like to use for
loop over result
as:
@for (var key = 0; key < result.value.length; key )
{
}
But C# thinks .length
is part of the PullRequestModel, and the code is not working.
How do I get around this problem?
This is the PullRequestModel
namespace TestApp.Model.PullRequestModel
{
public class Avatar
{
public string href { get; set; }
}
public class CompletionOptions
{
public string mergeCommitMessage { get; set; }
public bool deleteSourceBranch { get; set; }
public bool squashMerge { get; set; }
public string mergeStrategy { get; set; }
public bool triggeredByAutoComplete { get; set; }
public List<object> autoCompleteIgnoreConfigIds { get; set; }
}
public class CreatedBy
{
public string displayName { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public string id { get; set; }
public string uniqueName { get; set; }
public string imageUrl { get; set; }
public string descriptor { get; set; }
public string href { get; set; }
}
public class Iterations
{
public string href { get; set; }
}
public class LastMergeCommit
{
public string commitId { get; set; }
public string url { get; set; }
}
public class LastMergeSourceCommit
{
public string commitId { get; set; }
public string url { get; set; }
}
public class LastMergeTargetCommit
{
public string commitId { get; set; }
public string url { get; set; }
}
public class Links
{
public Avatar avatar { get; set; }
public Self self { get; set; }
public Repository repository { get; set; }
public WorkItems workItems { get; set; }
public SourceBranch sourceBranch { get; set; }
public TargetBranch targetBranch { get; set; }
public Statuses statuses { get; set; }
public SourceCommit sourceCommit { get; set; }
public TargetCommit targetCommit { get; set; }
public MergeCommit mergeCommit { get; set; }
public CreatedBy createdBy { get; set; }
public Iterations iterations { get; set; }
}
public class MergeCommit
{
public string href { get; set; }
}
public class Project
{
public string id { get; set; }
public string name { get; set; }
public string state { get; set; }
public string visibility { get; set; }
public DateTime lastUpdateTime { get; set; }
}
public class Repository
{
public string id { get; set; }
public string name { get; set; }
public string url { get; set; }
public Project project { get; set; }
public string href { get; set; }
}
public class Reviewer
{
public string reviewerUrl { get; set; }
public int vote { get; set; }
public bool hasDeclined { get; set; }
public bool isRequired { get; set; }
public bool isFlagged { get; set; }
public string displayName { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public string id { get; set; }
public string uniqueName { get; set; }
public string imageUrl { get; set; }
public bool isContainer { get; set; }
public List<VotedFor> votedFor { get; set; }
}
public class PullRequestModel
{
public List<Value> value { get; set; }
public int count { get; set; }
}
public class Self
{
public string href { get; set; }
}
public class SourceBranch
{
public string href { get; set; }
}
public class SourceCommit
{
public string href { get; set; }
}
public class Statuses
{
public string href { get; set; }
}
public class TargetBranch
{
public string href { get; set; }
}
public class TargetCommit
{
public string href { get; set; }
}
public class Value
{
public Repository repository { get; set; }
public int pullRequestId { get; set; }
public int codeReviewId { get; set; }
public string status { get; set; }
public CreatedBy createdBy { get; set; }
public DateTime creationDate { get; set; }
public DateTime closedDate { get; set; }
public string title { get; set; }
public string description { get; set; }
public string sourceRefName { get; set; }
public string targetRefName { get; set; }
public string mergeStatus { get; set; }
public bool isDraft { get; set; }
public string mergeId { get; set; }
public LastMergeSourceCommit lastMergeSourceCommit { get; set; }
public LastMergeTargetCommit lastMergeTargetCommit { get; set; }
public LastMergeCommit lastMergeCommit { get; set; }
public List<Reviewer> reviewers { get; set; }
public List<object> labels { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public CompletionOptions completionOptions { get; set; }
public bool supportsIterations { get; set; }
public DateTime completionQueueTime { get; set; }
}
public class VotedFor
{
public string reviewerUrl { get; set; }
public int vote { get; set; }
public string displayName { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public string id { get; set; }
public string uniqueName { get; set; }
public string imageUrl { get; set; }
public bool isContainer { get; set; }
}
public class WorkItems
{
public string href { get; set; }
}
}
This is the cause of the whole problem, if anyone is curious:
@if(result == null)
{
<p> No pull requests found</p>
}
else
{
@foreach (var item in result.value)
{
<div >
<input type="checkbox" id="tab1">
<label for="tab1"> @item.title </label>
<div >
<WorkItems PersonalAccessToken="@PersonalAccessToken" WorkItemUrl="@item._links.workItems.href"></WorkItems>
</div>
</div>
}
}
The above code works (it's a Blazor app) I loop trough all Pull requests and show associated Work Items for the PR.
But In order to crete a simple accordion, the I must increment id="tab1">
because in each loop, so my idea was to ditch @foreach
and use @for
loop, so I can make use of the key
to increment the value of id="tab1">
Hope it makes sense
CodePudding user response:
result = JsonConvert.DeserializeObject<PullRequestModel>(responseData);
@for (var key = 0; key < result.value.Count; key )
{
}
List<> exposes the Count property not Length property.
CodePudding user response:
It would appear you are deserializing a single object and not an enumerable type. Please try using the following if you want to return something to loop through:
result = JsonConvert.DeserializeObject<List<PullRequestModel>>(responseData);
@for (var key = 0; key < result.Count; key )
{
}
Count is a property of of the List object in this case.