2016-04-28 100 views
3

我有一個控制器,它有一個Create操作。其目的是從文件形式接收名稱和數據,並且IndexViewModel返回IEnumerable<File>文件。如何通過Angular2發送數據到ASP.NET控制器

public class HomeController : Controller 
{ 
    static List<Models.File> files = new List<Models.File>(); 

    public HomeController() { } 

    [HttpGet] 
    public IActionResult Index() => View(new IndexViewModel { Files = files }); 

    [HttpGet] 
    public IActionResult Create() => View(); 

    [HttpPost] 
    public IActionResult Create(IFormFile file) 
    { 
     var filename = 
      ContentDispositionHeaderValue.Parse(file.ContentDisposition) 
             .FileName; 

     using (var reader = new StreamReader(file.OpenReadStream())) 
     { 
      var content = reader.ReadToEnd(); 
      files.Add(new Models.File { Name = filename, Data = content }); 
     } 

     return RedirectToAction(nameof(Index)); 
    } 
} 

當我使用靜態的HTML 一個形式,這是正常的,服務器接收數據。但是,當我在模板01212中使用相同的表單時,它不會。

import {Component} from 'angular2/core'; 
import {File} from './file'; 


@Component({ 
    selector: 'listfile', 
    template: ` 
<form method="post" asp-action="Index" asp-controller="Api/File" enctype="multipart/form-data"> 
    <input type="file" name="files" #newFile (keyup.enter)="addFile(newFile.value)" 
    (blur)="addFile(newFile.value); newFile.value='' "> 
    <input type="submit" value="Upload" /> 
</form> 
    <table class="table"> 
     <th>id</th><th>name</th> 
     <tr *ngFor="#file of files"> 
      <td>{{file.id}}</td> 
      <td>{{file.name}}</td> 
     </tr> 
</table> 
    ` 
}) 
export class ListFileComponent { 
    files = [ 
     new File(1, 'file1'), 
     new File(2, 'file2') 
    ]; 
    addFile(newFile: string) { 
     if (newFile) { 
      this.files.push(new File(this.files.length + 1, newFile.split(/(\\|\/)/g).pop())) 
     } 
    } 
    falert(value) { 
     alert(value); 
    } 
} 

回答

4

你有Angular2模板的誤解和MVC預處理。 Here is a post這可能會清除。您有ASP.NET標籤幫助程序不會在服務器上呈現,而是按原樣發送到客戶端。

您正在使用form post which passes form data,相反,你應該使用的Web APIAngular2的Http服務。

你有很多選擇,但他們兩個是最實用的,在這一點上:

  1. 您可以選擇使用電源的MVC其前處理,並有局部視圖返回形成。在您的組件使用templateUrl代替template並指向/controller/action,將預處理的標籤助手並返回HTML根據需要(那麼這將作爲一個Angular2模板。
  2. 你可以開始利用Angular2作爲一個真正的SPA,並且只能使用MVC爲有史以來的單個頁面服務... /home/index然後你使用templateUrl指向本地的.html作爲模板的文件,並且構建一個支持所有你需要的互動

我希望這可以解決問題!


如果您要使用內聯或靜態 .html您需要刪除 ASP.NET標籤助手。

@Component({ 
    selector: 'listfile', 
    template: ` 
<form (submit)="" > 
    <input type="file" name="files" #newFile (keyup.enter)="addFile(newFile.value)" 
    (blur)="addFile(newFile.value); newFile.value='' "> 
    <input type="submit" value="Upload" /> 
</form> 
    <table class="table"> 
     <th>id</th><th>name</th> 
     <tr *ngFor="#file of files"> 
      <td>{{file.id}}</td> 
      <td>{{file.name}}</td> 
     </tr> 
</table> 
    ` 
}) 
export class ListFileComponent { 
    // ... 
    constructor(private http: Http) { } 
    onSubmit() { 
     const body = JSON.stringify({ this.files }); 
     this.http 
      .post('api/file/create', 
       body, 
       { headers: new Headers({ "Content-Type": "application/json" }) }) 
      .map(response => response.json()) 
      .subscribe(json => { /* handle it */ }); 
    } 
} 

然後,您必須更改您的API簽名才能更準確地接受數據。

+0

感謝您的回答,明白了,好文章! 我認爲第二個選擇更好。 –

相關問題