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 }); }
In MVC, we can access data from database easily. When we send any request to the server, respond generated by the controller. Data access from the Model and render by the View. First to make a database table using visual studio 2010 or higher. we have database table that is "Employee", in which have some attributes, these are
CREATE TABLE [dbo].[Employee] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY, [Name] NVARCHAR(50) NULL, [Age] INT NULL )
- First to install entity framework by the "Nuget packages", which is available in tools-->Library Package Manager
- This is already available in 4.5 framework or 4.5.1.
- Now, add the employee class in model folder with some public properties like
-
namespace WebApplication10.Models { public class Employee { public int ID { get; set; } public string Name { get; set; } public int age { get; set; } } }
- Now, add another class which is EmployeeContext.cs class, which is inherit from DBContext class. DBContext class is available in System.Data.Entity namespace. This class is used for making connection. Now, your code look like
-
using System.Data.Entity;namespace WebApplication10.Models { public class EmployeeContext: DbContext { public DbSet<Employee> empl { get; set; }} }
Here, empl is the public property, by which we can create the connection to the database. - Now, add the ConnectionString in the web.config file that is
<add name="EmployeeConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Initial Catalog=Database1;Integrated Security=True" providerName="System.Data.SqlClient" />
- Map the database table with the Employee class using Table class which is available in System.ComponentModel.DataAnnotations.Schema; namespace, after mapping now your code look like
using System.ComponentModel.DataAnnotations.Schema;namespace WebApplication10.Models {[Table("Employee")] public class Employee { public int ID { get; set; } public string Name { get; set; } public int age { get; set; } } }
- Now, create a EmployeeController in controller folder, Which is inherit from Controller class. This Controller class is exists in System.Web.Mvc namespace. Also create a public method, which return ActionResult class. Now your code look like
using System.Linq; using System.Web.Mvc; using WebApplication10.Models; namespace WebApplication10.Controllers { public class EmployeeController : Controller { // // GET: /Employee/ public ActionResult Index() { EmployeeContext context = new EmployeeContext(); Employee emp = context.pl.Single(); return View(emp); } } }
Here we create a instance of EmployeeContext class and invoke the public property of that class. - Now create the view for this program. Before adding the view with their presentation must build the project. Learn How to create the new view
- Prepare the view and access all the properties of the employee class by the @Model properties.
@model WebApplication10.Models.Employee @{ ViewBag.Title = "Employee Details"; } <h2>Employee Details are:</h2> Employee Id : <p>@Model.ID</p> Employee Name : <p>@Model.Name</p> Employee Age : <p>@Model.age</p>
- Now run the code and check your desired output
Comments
Post a Comment