I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component public class AllViewComponent : ViewComponent { private readonly UserManager<ApplicationUser> _userManager; public AllViewComponent(UserManager<ApplicationUser> userManager) { _userManager = userManager; } public async Task<IViewComponentResult> InvokeAsync() { List<StudentViewModel> allUsers = new List<StudentViewModel>(); var items = await _userManager.Users.ToListAsync(); foreach (var item in items) { allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email }); }
Introduction
In my previous post we have been learned How to create a new controller in MVCBydefault a controller class contains a Index method and that method returns a ActionResult type value. So we can return View in Index method. Here we will use ViewBag object for initializing dynamic properties such as
public class HomeController : Controller
{
//
// GET:
/Home/
public ActionResult Index()
{
ViewBag.name = "jacob";
return View();
}
}
If we want to pass Data ("jacob") from Controller class to View then first we would create a View.How to create a View in View folder
Step-1 : Create a Directory which name same as Controller class ( here we use Home)Step-2: Right Click on Home Directory and add new View which name same as Action Controller method (here we use Index.cshtml)
Step-3: Open Index.cshtml page and access Dynamic properties of View Bag object using "@" symbol such as
<h2>@ViewBag.name</h2>
Note :Must Check your Directory structure
Comments
Post a Comment