简体   繁体   English

Angular 2 post 数据在 asp.net MVC 6 中为空

[英]Angular 2 post data is null in asp.net MVC 6

At the moment I'm using Angular 2 Beta-9 together with Asp.Net MVC 6 .目前我正在使用Angular 2 Beta-9Asp.Net MVC 6 I'm trying to create a simple contact form as test.我正在尝试创建一个简单的联系表单作为测试。 The problem is that my form data does not seem to get passed to the server, while everything seems to be going ok on the Angular side.问题是我的表单数据似乎没有传递到服务器,而在 Angular 方面似乎一切正常。

contact.ts联系人.ts

/// <reference path="../node_modules/angular2/typings/browser.d.ts" />

import {Component, View} from 'angular2/core';
import {NgForm}    from 'angular2/common';
import {bootstrap} from 'angular2/platform/browser';
import {Http, Headers, HTTP_PROVIDERS} from 'angular2/http';

@Component({
    selector: 'contact',
    bindings: [ContactComponent],
    viewProviders: [HTTP_PROVIDERS],
    templateUrl: '/angular/contact'
})

export class ContactComponent {
    http = undefined;
    contact = {};

    constructor(http: Http) {
        this.http = http;
    }

    onSubmit() {
        this.http.post('/contact/send', JSON.stringify(this.contact), new Headers({ 'Content-Type': 'application/json' })).subscribe();
    }
}

bootstrap(ContactComponent);

Contact.cshtml联系方式.cshtml

<form #f="ngForm" (ngSubmit)="onSubmit(contact)">
        <div>
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" [(ngModel)]="contact.Name" class="form-control text-input" id="name" placeholder="Name" />
            </div>
            <div class="form-group">
                <label for="email">E-mail</label>
                <input type="email" [(ngModel)]="contact.Email" class="form-control text-input" id="email" placeholder="E-mail" />
            </div>
        </div>
        <div>
            <div class="form-group">
                <label for="subject">Subject</label>
                <input type="text" [(ngModel)]="contact.Subject" class="form-control text-input" id="subject" placeholder="Subject" />
            </div>
        </div>
        <div>
            <div class="form-group">
                <label for="message">Bericht</label>
                <textarea type="text" [(ngModel)]="contact.Message" class="form-control" id="message" placeholder="Message"></textarea>
            </div>
        </div>
        <div>
            <div>
                <button type="submit" class="btn btn-success">Send</button>
            </div>
        </div>
    </form>

ContactController.cs联系人控制器.cs

[HttpPost]
public IActionResult SendContact([FromBody]ContactVm contact)
{
    //do something
}

ContactVm.cs联系方式

public class ContactVm
    {
        [Required]
        [DataType(DataType.Text)]
        public string Name { get; set; }
        [Required]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }
        [Required]
        [DataType(DataType.Text)]
        public string Subject { get; set; }
        [Required]
        [DataType(DataType.MultilineText)]
        public string Message { get; set; }
    }

I can't see or find anything I'm doing wrong.我看不到或找不到我做错了什么。 Untill the http.post , this.contact is filled in like it should, but once it reaches the server it's null .直到http.postthis.contact都像它应该的那样填写,但是一旦它到达服务器,它就是null

UPDATE更新

Found that the Request.Form is throwing the following error:发现Request.Form抛出以下错误:

Incorrect Content-Type: text/plain;charset=UTF-8

From what I have found, MVC will "fail silently" if it cannot force your posted value into the parameter.根据我的发现,如果 MVC 无法将您发布的值强制到参数中,它将会“静默失败”。

So, while I am developing I used "object" - which ensures I get ALL posts.所以,在我开发时,我使用了“对象”——这确保我得到所有帖子。 And then I fine tune it back to refer to the correct object.然后我微调它以引用正确的对象。

Also, I found that (in Angular 6 which I am using) you definitely don't need to JSON.stringify the object... Look at the Chome Inspect tab, at the POST, an then Preview, and it will give you some detail of the failing...此外,我发现(在我使用的 Angular 6 中)你绝对不需要 JSON.stringify 对象......查看 Chome Inspect 选项卡,在 POST,然后预览,它会给你一些失败的细节...

let headers = new Headers({ 'Content-Type': 'application/json'});

this.http.post('/contact/send',JSON.stringify(this.contact),{ headers: headers })
         .subscribe(...);

server side .服务器端

[HttpPost]
public IActionResult SendContact(ContactVm contact)
{
    //do something
}

If it doesn't work also see below selected answer.如果它不起作用,请参阅下面选择的答案。

try this:尝试这个:

getJsonHeader(): Headers {
        var headers = new Headers();
        headers.append('Content-Type', 'application/json; charset=utf-8');
        return headers;
    }

   onSubmit() {
        this.http.post('/contact/send', JSON.stringify(this.contact), { headers: this.getJsonHeader() }).subscribe();
    }

I don't think you need JSON.stringify:我认为你不需要 JSON.stringify:

this.http.post('/contact/send', this.contact, new Headers({ 'Content-Type': 'application/json' })).subscribe(); this.http.post('/contact/send', this.contact, new Headers({ 'Content-Type': 'application/json' })).subscribe();

This corrected my issue HTH这纠正了我的问题 HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM