【问题标题】:saving document via services with Angular 7使用 Angular 7 通过服务保存文档
【发布时间】:2019-06-09 00:18:02
【问题描述】:

您好,我正在使用 angular 7 应用程序使用 asp.net mvc。

我正在尝试将上传的文档保存到数据库中。

这是保存数据的方法:

export class EditDiplomaComponent extends AfwComponent implements OnInit {
    resources: DiplomasWijzigenResourcesModel;
    private readonly apiDiploma: 'api/register/teachers/current/diploma';
    readonly resourceKeys = DiplomasWijzigenResourcesModel.keys;
    buttons: ImmutableArrayType<ButtonModel>;
    protected readonly formName = FormNamesModel.diplomaForm;
    formModel: AfwFormService;
    documentList?: DocumentListModel;
    documentListCompoment: DocumentListComponent;
    diplomaFormKeysModel: DiplomaFormKeysModel;
    protected baseDocumentsUrl: string;
    teacher: TeacherModel;


    constructor(
        private readonly http: AfwHttp,
        readonly route: ActivatedRoute,
        readonly textResourceService: TextResourceService,
        private readonly formService: FormService,
        private readonly multiFileUploadControlFactory: MultiFileUploadControlFactory,
        private readonly feedbackService: FeedbackStoreService,
        private readonly router: Router,


    ) {
        super(textResourceService, route);
    }

    ngOnInit(): void {
        // this.baseDocumentsUrl = `api/register/teachers/current/diploma/${this.teacher.guid}/documents?fileName=`;
        const defaultModel = diplomaFormModel(this.multiFileUploadControlFactory);

        this.formModel = this.formService.createModel<DiplomaFormModel, DiplomasWijzigenResourcesModel>(
            defaultModel, { qualificationTerms: null, diplomasUpload: this.multiFileUploadControlFactory.createEmptyDefaultValues() }, this.resources
        );

        const saveBtn = new ActionButtonModel({ resourceKey: this.resourceKeys.opslaanKnop, isSubmit: true });
        const cancelBtn = new CancelLinkButtonModel();
        this.buttons = ImmutableArrayType.Create([saveBtn, cancelBtn]);
    }

    diplomaDataSubmitted($event: AfwFormService) {

        // const formModel = $event;
        if (this.allowSubmitAgain && this.formModel.form.valid) {

            const obs = this.saveDiploma(true, this.resources.opslaanSpinnerTekst).pipe(map(() => {
                this.router.navigateByUrl(RoutesRegisterModel.pathnameMyRegisterRouteDiplomas).then(() => {
                    this.feedbackService.addSuccessMessage (
                        {
                            feedbackPortalKey: FeedbackPortalsModel.mainFeedback,
                            message: { key: 'succesvolWijziging', value: this.resources.succesvolWijziging },
                        });
                });
            }))
            this.submitAllowed(obs);
        }
    }

    saveDiploma(competenceStatement: boolean, spinnerMessage: string): Observable<any> {

        return this.http.post(`${this.apiDiploma}`,
            competenceStatement,
            spinnerMessage);
    }
}

这是文凭控制器:

[Authorize(Roles = IdentityRoles.Teacher)]
    [RoutePrefix("api/register/teachers/current/diploma")]
    public sealed class DiplomaController : ApiControllerBase
    {
        private readonly IDiplomaProcess _diplomaProcess;

        /// <summary>
        /// Constructor.
        /// </summary>
        public DiplomaController(IDiplomaProcess diplomaProcess)
        {
            _diplomaProcess = diplomaProcess;
        }

        /// <summary>
        /// Create the diploma record for the current teacher.
        /// </summary>
        /// <param name="competenceStatement">Has agreed to the competence statement?</param>
        [Route("")]
        [HttpPost]
        public void CreateDiploma(bool competenceStatement)
        {
            _diplomaProcess.CreateDiploma(competenceStatement);
        }

        /// <summary>
        /// Delete the diploma record for the current teacher.
        /// </summary>
        [HttpDelete]
        [Route("")]
        public void DeleteDiploma()
        {
            _diplomaProcess.DeleteDiploma();
        }

        /// <summary>
        /// Get the diploma documents of the current teacher.
        /// </summary>
        /// <returns>The DocumentListDto with the diploma documents.</returns>
        [Route("")]
        [HttpGet]
        public DiplomaDto GetDiplomas()
        {
            return _diplomaProcess.GetDiplomas();
        }
}

但是当我调试代码时,我在这一行看到:

 return this.http.post(`${this.apiDiploma}`, 

apDiploma 未定义。我正在使用 Observable。

但我认为您必须订阅它。但是如何订阅方法呢?

谢谢

谢谢。 但在这一行:return this.http.post(${this.apiDiploma},

我看到了正确的url: "api/register/teachers/current/diploma"

这是输出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<DiplomaDto xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/VLR.BackEnd.Dto.Teacher">
<Documents xmlns:d2p1="http://schemas.datacontract.org/2004/07/VLR.BackEnd.Dto.Document">
<d2p1:Documents/>
<d2p1:DocumentsInQueue>-2147483648</d2p1:DocumentsInQueue>
<d2p1:IsSucceeded>true</d2p1:IsSucceeded>
</Documents>
<HasAgreedToCompetenceStatement>false</HasAgreedToCompetenceStatement>
</DiplomaDto>

但是如果调试后端(控制器)

那我看不是这个方法:

[Route("")]
        [HttpPost]
        public void CreateDiploma(bool competenceStatement)
        {
            _diplomaProcess.CreateDiploma(competenceStatement);
        }

【问题讨论】:

  • 有人能帮帮我吗?

标签: asp.net asp.net-mvc angular web-services


【解决方案1】:

我想你要做的就是将它定义为一种字符串类型并为其赋值

将该行更改为

private readonly apiDiploma: string = 'api/register/teachers/current/diploma';

【讨论】:

  • 我的方法不允许
  • 我收到此错误:{"message":"请求的资源不支持 http 方法 'POST'。"}
猜你喜欢
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 2020-10-15
相关资源
最近更新 更多