例如,我有ViewComponent BookShelf,其主要思想是將書籍添加到書架上。組件文件:ViewComponent和他的控制器之間的交互
(〜/查看/共享/組件/書架/ Default.cshtml)
<form id="addForm" asp-controller="?" asp-action="?" role="form" method="post">
<input type="text" name="bookName" value="exampleValue"/ >
<button type="submit"> Add book </button>
</form>
(〜/ ViewComponents/BookShelfViewComponent.cs)
public class BookShelfViewComponent : ViewComponent
{
private readonly ApplicationDbContext _dbContext;
public RoleManagementViewComponent(
ApplicationDbContext context)
{
_dbContext = context;
}
public IViewComponentResult Invoke()
{
return View();
}
public IViewComponentResult AddBook(string bookName)
{
//Some database logic to add book
}
}
所以,主要問題是如何在此ViewComponent中將書名傳遞給AddBook方法?什麼應該在asp-控制器和asp-action屬性?也許我不應該返回IViewComponentResult,如果我想在添加書籍後重新加載ViewComponent?
是BookShelfViewComponent控制器? – Imad
@Anonymous這是默認控制器_BookShelf/Default.cshtml_ –