2016-12-05 96 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的某些特殊功能。

相关问题