をコントローラのアクションを行います。は、フォームを送信し、私は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)
しかし、私はこの仕事を得ることができません。何も追加されません。
ホーこのViewComponentを呼びますか? { ViewDataを[ "タイトル"] = "ホームページ" @ – Dealdiane
。 } @ Component.Invoke( "AddListItem"、@ ViewBag.DefaultListId) –