2016-04-28 106 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

感谢您的回答,明白了,好文章! 我认为第二个选择更好。 –

相关问题