2016-02-14 6 views
7

をコントローラのアクションを行います。は、フォームを送信し、私はASP.NET 5でViewComponent、MVCのコアアプリケーションのフォームからのListItemを追加したいASP.NETコアにViewComponentから

コンポーネントビュー(ビュー\共有\コンポーネント\ AddListItem \ Default.cshtml):

@model ShoppingList.Models.ListItem 
<form asp-action="Create"> 
    <div class="form-horizontal"> 
     <hr /> 
     <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div> 
     <!-- some fields omitted for brevity --> 
     <div class="form-group"> 
      <label asp-for="Description" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Description" class="form-control" /> 
       <span asp-validation-for="Description" class="text-danger" /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
</form> 

ViewComponentコントローラ(AddListItem.cs \ ViewComponents):コンポーネントが呼び出される

namespace ShoppingList.ViewComponents 
{ 
    public class AddListItem : ViewComponent 
    { 
     private readonly ApplicationDbContext _context; 

     public AddListItem(ApplicationDbContext context) 
     { 
      _context = context; 
     } 

     public IViewComponentResult Invoke(string listId) 
     { 
      return View(); 
     } 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<IViewComponentResult> Create (ListItem listItem) 
     { 
      _context.ListItem.Add(listItem); 
      await _context.SaveChangesAsync(); 
      return View(listItem); 
     } 

    } 
} 

home.cshtmlに:

@{ 
     ViewData["Title"] = "Home Page"; 
} 

@Component.Invoke("AddListItem", @ViewBag.DefaultListId) 

しかし、私はこの仕事を得ることができません。何も追加されません。

+0

ホーこのViewComponentを呼びますか? { ViewDataを[ "タイトル"] = "ホームページ" @ – Dealdiane

+0

。 } @ Component.Invoke( "AddListItem"、@ ViewBag.DefaultListId) –

答えて

3

名前の変更AddListItemViewComponentからAddListItem。 ViewComponentsはViewComponent接尾辞で終了する必要がある - これは、コンポーネントを見つけるために、ASP.NETによって使用される規則です。あなたは、このようにそれを行うにはしたくない場合は、[ViewComponent]属性を持つクラスを飾ると、必要な任意の名前に名前プロパティを設定することができます。

Createメソッドも呼び出されることはなく、ViewComponentsはHttPostに決して応答しません。なぜなら、表示を表示し、ポストバックに応答してコードを実行しないことしかないからです。ビューは、Invokeまたはその非同期バージョンInvokeAsyncを呼び出すだけです。

最初は少し混乱だが、それについて考えるための簡単な方法は、DIは友好的、あなたがコンポーネントクラスでより多くのものを行うことができます意味で強力なPartialようなもので、同様にテストすることが容易です。

ASP.NETチームはViewComponentsあなたの助け、@Dealdianeためhere

+0

リンクが変更されたか、そのセクションが使用できなくなったと思います... https://docs.microsoft.com/en-us/aspnet/ core/mvc/views/view-components – JMG

+0

あなたはviewcomponentでHttpPostメソッドを呼び出せないことを意味しますか? –

+0

@AProgrammer、それは正しいです。 ViewComponentsは、要求を処理するのではなく、要素をレンダリングするように設計されています。 – Dealdiane

2

感謝を説明するページがあります。

誰もがここで、intested場合は、作業コードは次のとおりです。

ビューは共有\コンポーネント\ AddListItem \ Default.cshtml

<form asp-controller="ListItems" asp-action="QuickCreate"> 
    <div class="form-horizontal"> 
     <hr /> 
     <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div> 

     <div class="form-group"> 
      <label asp-for="No" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="No" class="form-control" /> 
       <span asp-validation-for="No" class="text-danger" /> 
      </div> 
     </div> 
... 

コントローラ\ ListItemsController.cs

// POST: ListItems/QuickCreate 
    // Create item without showing view, return to home 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<IActionResult> QuickCreate(ListItem listItem) 
    { 
      _context.ListItem.Add(listItem); 
      await _context.SaveChangesAsync(); 
      return Redirect("/"); 
    } 

\します作業中のプロジェクトの完全なソースコードはここで見つけることができる:GitHub ShoppingList

関連する問題