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
ViewData is used to pass Data from controller to View in MVC. ViewData is a Dictionary of Objects that are stored and retrieved using string as a Keys such as
ViewData["Key"] = Value; // Stored value in ViewData.
String n = ViewData["Keys"] ; // retrieve from DataView
Controller class code (HomeController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
//
// GET:
/Home/
public ActionResult Index()
{
ViewData["countries"] = new List<string>()
{
"USA",
"INDIA",
"UK"
};
return View();
}
}
}
@{
ViewBag.Title = "List of string value";
}
<h2>Country List</h2>
<ul>
@foreach (string country in (List<string>)ViewData["countries"])
{
<li>@country</li>
}
</ul>
Comments
Post a Comment