Skip to main content

Featured Post

How to use Tabs in ASP.NET CORE

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 });             }            

Data Binding in MVC

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
)
  1. First to install entity framework by the "Nuget packages", which is available in tools-->Library Package Manager
  2. This is already available in 4.5 framework or 4.5.1.
  3. Install Entity Framework By the nuget packages
  4. 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; }
    }
    }
    
    
  5. 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.
  6. 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" />
    
    
  7. 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; }
    }
    }
    
  8. 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.
  9. 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

Before adding the view with their presentation must build the project


  1. 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>
    
    
  2. Now run the code and check your desired output

Data Binding in MVC

Comments

Popular Post

How to use Tabs in ASP.NET CORE

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 });             }            

Polynomial representation using Linked List for Data Structure in 'C'

Polynomial representation using Linked List The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows: Polynomial:      3x 3 -4x 2 +2x-9 Linked List: In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The first node of the linked list contains the information about the variable with the highest degree. The first node points to the next node with next lowest degree of the variable. Representation of a polynomial using the linked list is beneficial when the operations on the polynomial like addition and subtractions are performed. The resulting polynomial can also

Print the asp.net webpage using button

Introduction Today i am talking about printing, and how to print the content or you can say selected content in asp.net. If you have a webpage and you want to print this then you have to choose command key (ctrl+p) for that. Also you want to print the selected part then you have to select the text first then you have to use command key. But sometimes your selected page could not printed. So many websites provide the printing facilities on the webpage. Today i am talking about the same topics here. Lets take a simple example to demonstrate the topic. Source code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="printpage.aspx.cs" Inherits="printpage" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title></title> <script> function printpage() { var getpanel = document.getElementById("<%= Panel1.ClientID%>"); var MainWin