BindModelWithController
Package
"Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2"
Controllers[Folder]
HomeController.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BindModelWithController.Models;
namespace BindModelWithController.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
Student std1 = new Student()
{
id = 1,
name = "Adil1",
age = 18
};
Student std2 = new Student()
{
id = 2,
name = "Adil2",
age = 18
};
//Generic List
List<Student> std_list = new List<Student>();
std_list.Add(std1);//it is indexing of list
std_list.Add(std2);
return View(std_list);
}
}
}
Models[Folder]
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BindModelWithController.Models
{
public class Student
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
}
Views
Home->Index
@model IEnumerable<BindModelWithController.Models.Student>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.id)
</th>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.age)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.age)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
0 Comments