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 i have been learned about pass data from controller to view in MVC. This post contains a list of string value in controller action method and we want to pass that value to MVC View.lets take a simple Example.
Step-1 : Create a List<String> collection and pass these collection to Dynamic property of ViewBag object.Step-2: Return View in Controller Index method.
Step-3: Create a view and access dynamic property of ViewBag Object from Controller Index method.
Controller class
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()
{
ViewBag.country = new List<string>()
{
"USA",
"INDIA",
"UK"
};
return View();
}
}
}
View
@{
ViewBag.Title = "List of string value";
}
<h2>Country List</h2>
<ul>
@foreach (string country in ViewBag .country)
{
<li>@country</li>
}
</ul>
Comments
Post a Comment