2016-12-05 82 views
1

我想從angularjs2發佈數據到asp.net mvc控制器。Angular 2將數據發佈到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#類

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; 
     } 

    } 

任何幫助讚賞

+0

你得到了什麼錯誤? – brando

回答

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。顯然,你可以修改一些Var和IGenericRestResponse來滿足你自己的需求。

在客戶端,您也可以返回Observable而不是promise; promise方法對我來說比較熟悉,所以我使用它,除非我需要Observable的某些特殊功能。