I have implemented an object on the server side (asp.net webapi) controller that contains boolean
value and IEnumerable
list. The controller method returns this object. I have also implemented an interface on the client side angular to match the object. However I am getting error:
documentDetails
does not contain a propertydocumentType
.
Not sure why I am getting this error when the interface has it.
Server Side:
public class DocumentTypeViewModel {
public IEnumerable<DOCUMENT_TYPE> documentType;
public bool canView {
get;
set;
}
}
[System.Web.Http.HttpGet]
public DocumentTypeViewModel GetDocumentTypes() {
var documentTypeViewModel = new DocumentTypeViewModel() {
canView = (IoC.Resolve<IClientAuthorizationService>().Authorize("Put", "ResearchPanel") == AuthAccessLevel.Full),
documentType = IoC.Resolve<IRepo<DOCUMENT_TYPE>>().GetAll().Where(x => x.IS_ACTIVE)
.OrderBy(t => t.SORT_ORDER)
};
return documentTypeViewModel;
}
If you see below documentDetails
is declared of the type IDocumentTypes
and also initialized.
export interface IDocumentTypes {
canView: boolean;
documentType: any;
}
documentDetails: IDocumentTypes[] = [{
canView: false,
documentType: null
}];
this.documentService.getDocumentTypes()
.subscribe((data: any) => {
this.documentDetails = data;
this.DocumentTypes = this.documentDetails.documentType.filter(x => x.IS_ACTIVE)
.map(o => {
return new ListItem(o['ID'], o['NAME'], true)
});
this.SelectedDocTypeIds = this.DocumentTypes.map(o => {
return o['value']
});
this.populateStrategies();
},
err => {
this.Error = 'An error has occurred. Please contact BSG';
},
() => {})
Screenshot - console.log(this.DocumentDetails)
this.documentDetails
is an Array. The documentType
Object is present at the 0th index of that array.
So you'll have to access it like this: this.documentDetails[0].documentType.(...)
Also, you're trying to access properties that don't really exist on the Objects in the documentType
Array.
Give this code a try. It should work:
import { Component, OnInit } from '@angular/core';
export interface IDocumentTypes {
canView: boolean;
documentType: any;
}
class ListItem {
value: number;
text: string;
selected: boolean;
constructor(value: number, text: string, selected: boolean) {
this.value = value;
this.text = text;
this.selected = selected;
}
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
name = 'Angular';
documentDetails: IDocumentTypes[] = [{ canView: false, documentType: null }];
DocumentTypes: Array<ListItem> = new Array<ListItem>();
ngOnInit() {
this.loadDocuments();
}
private loadDocuments() {
this.documentDetails = documents;
this.DocumentTypes = this.documentDetails[0].documentType.filter(x => x.IsActive)
.map(o => { return new ListItem(o['DocumentTypeId'], o['Name'], true) });
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.