2017-04-07 13 views
0

私は_Layout.cshtmlにビューコンポーネントを持っています。私のアプリケーションのルートは/home/{id}です。 View Component ControllerからURLルートのid値を取得するにはどうすればよいですか?.NET CoreのView Componentコントローラからルート値を取得するにはどうすればよいですか?

public class LayoutViewComponent : ViewComponent 
{ 
    public async Task<IViewComponentResult> InvokeAsync() 
    { 
     //how do I get the value of {id} here? 

     return View(); 
    } 
} 

答えて

1

ビューを移動して、クエリ文字列から必要なパラメータを単独で取得する必要はありません。これは、ビューの使用を厳密にすることができます。

代わりに、親からIdを渡すことができます。

// Parent's Action Method 
public IActionResult ParentActionMethod(int id) 
{ 
    // You could use strongly typed model 
    ViewBag.Id = 1; 
    return View(); 
}  

// Parent's View 
@await Component.InvokeAsync("Layout", new { Id = ViewBag.Id }) 

// View Component 
public class LayoutViewComponent : ViewComponent 
{ 
    public async Task<IViewComponentResult> InvokeAsync(int id = 10) 
    { 
     return View(); 
    } 
} 
関連する問題