Angular 2将数据发布到asp.net mvc控制器

时间:2016-12-05 17:38:16

标签: c# asp.net-mvc angular

我正在尝试将数据从angularjs2发布到asp.net mvc控制器。

实际问题是,当我尝试使用它时,

看我怎么努力?

这是打字稿---

 save(company: Company): Observable<boolean> {     
        let headers = new Headers({ 'Content-Type': 'application/json' });
         this._http.post(this._postUrl, /*JSON.stringify(*/company/*)*/, { headers: headers }) 
            .subscribe(
             (data) => {                 
                console.log('Response');               
                new Observable<true>()
            },
             (err) => { console.log(err); new Observable<false>(); },
            () => console.log('Complete')
            );         

         return new Observable<false>();

          }

onSignUpClicked(message: string): void {
        this._service.save(this.company).subscribe(
           res => console.log(res),
            error => this.errorMessage = <any>error
        );

这是打字稿类:

import { Address } from '../shared/Address';
import { Contact } from '../shared/Contact';

export class Entity {
    Id: string;
    InsertionTime: Date;
    InsertUserId: number;
    IsDeleted: boolean;
    IsLocked: boolean;
    UpdateTime: Date;
    UpdateUserId: number;
}

export class Company extends Entity {  
    Name: string;
    Address: Address;
    Contact: Contact;
    Password: string;
    ConfirmPassword: string;
    UserName: string;
    RegistrationDate: Date;
    IsActive: boolean;
    NextBillingDate: string;
    TransactionLimit: number
}

和C#class

public class Company : Entity
    {       
        public string Name { get; set; }
        public Address Address { get; set; }
        public Contact Contact { get; set; }
        public string Password { get; set; }
        public string UserName { get; set; }
        public Image LogoImage { get; set; }
        public DateTime RegistrationDate { get; set; }
        public DateTime LastUpdated { get; set; }
        public bool IsActive { get; set; }
        public DateTime NextBillingDate { get; set; }
        public Int64 TransactionLimit { get; set; }
    }

 public class Entity : IEntity
    {
        public Entity()
        {
            Id = Guid.NewGuid();
            InsertionTime = DateTime.Now;
            IsDeleted = false;
            IsLocked = false;
        }

        public Guid Id
        {
            get;set;
        }

        public DateTime InsertionTime
        {
            get;set;
        }

        public int InsertUserId
        {
            get; set;
        }

        public bool IsDeleted
        {
            get; set;
        }

        public bool IsLocked
        {
            get; set;
        }

        public DateTime? UpdateTime
        {
            get;set;
        }

        public int? UpdateUserId
        {
            get; set;
        }

    }

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

以下是ng2应用程序对服务器的基本调用:

getMeSomeServerData(someVar: string): Promise < IGenericRestResponse > {
      let headers = new Headers();
      headers.append("Content-Type", "application/json");
      let url = "/getMeSomeServerData";
      let post = this.http.post(url, JSON.stringify(someVar), {
        headers: headers
      }).map(response => response.json());
      return post.toPromise();
    }

在asp.net mvc后端:

// this of course goes within a controller
 [HttpPost()]
 [Route("getMeSomeServerData")]
 public JsonNetResult GetMeSomeServerData(string someVar) {
   GenericRestResponse response = new GenericRestResponse();
   response.Error = false;
   // do somthing 
   return new JsonNetResult(response);
 }

JsonNetResult只是将对象序列化为json的自定义方法。显然,您可以根据自己的需要修改someVar和IGenericRestResponse。

在客户端,您也可以返回Observable而不是promise; promise方法对我来说比较熟悉,所以除非我需要一些Observable的特殊功能,否则我会使用它。