مقدمة الى ملف ViewImports في تطبيق ASP.NET Core MVC
-
مقدمة الى ملف ViewImports في تطبيق ASP.NET Core MVC
في هذا الدرس ان شاء الله بناقش ViewImports في تطبيقات ASP.NET Core MVC. حيث يعتبر هذا الملف Razor ViewImports إحدى الميزات الجديدة في Net Core. في هذاالدرس بنعرف على ميزات هذا الملف ولماذا يستخدم وكيفية انشاءه والتسلسل الهرمي.
تمام التمام جاهزين نبدا الدرس:
ما هو _ViewImports.cshtml في تطبيق ASP.NET Core MVC؟
هو عبارة عن ملف شبيه لملف ViewStart بيعمل على توفير الوقت والجهد في تطبيقات ASP.NET Core MVC ، بحيث يوفر هذا الملف آلية لتضمين بعض directives globally في صفحات View Razor واكيد هذا الامر بوفر علينا وقت وجهد بحيث ما نضطر إلى إضافتها بشكل فردي في كل صفحة. ال directives التي يدعمها ملفViewImports.cshtml _ هي:
@addTagHelper
@tagHelperPrefix
@removeTagHelper
@namespace
@inject
@model
@using
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace StudentsAcademy.Models
{
public class StudentsModel
{
public string CustomerNo { get; set; }
public string FullName { get; set; }
public DateTime Birthday { get; set; }
public int Gender { get; set; }
public int Nationality { get; set; }
public string Address { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public string Note { get; set; }
}
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using StudentsAcademy.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using StudentsAcademy.Services;
using StudentsAcademy.Share;
namespace StudentsAcademy.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private TotalStudents _TotalStudents;
ApplicationDbContext db;
public HomeController(TotalStudents TS)
{
_TotalStudents = TS;
}
public ViewResult AllStudent ()
{
List<StudentsModel> listStudents = new List<StudentsModel>()
{
new StudentsModel() { CustomerNo = "0001", FullName = "Hatem", Birthday = Convert.ToDateTime("10/10/1980"), Address = "A", Email = "[email protected]" },
new StudentsModel() { CustomerNo = "0002", FullName = "Ahmad", Birthday = Convert.ToDateTime("12/08/1978"), Address = "B", Email = "[email protected]" },
new StudentsModel() { CustomerNo = "0003", FullName = "Ali", Birthday = Convert.ToDateTime("01/12/1985"), Address = "A", Email = "[email protected]" },
new StudentsModel() { CustomerNo = "0004", FullName = "Layan", Birthday = Convert.ToDateTime("22/08/2011"), Address = "A", Email = "[email protected]" },
new StudentsModel() { CustomerNo = "0005", FullName = "Lareen", Birthday = Convert.ToDateTime("14/08/2014"), Address = "B", Email = "[email protected]" }
};
return View(listStudents);
}
public ViewResult Details(int Id)
{
var studentDetails = new StudentsModel() { CustomerNo = "0001", FullName = "Hatem", Birthday = Convert.ToDateTime("10/10/1980"), Address = "A", Email = "[email protected]" };
return View(studentDetails);
}
}
}


@model List<StudentsAcademy.Models.StudentsModel>
@{
Layout = null;
}
<html>
<head>
<title>Index</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Student No</th>
<th>FullName</th>
<th>Birthday</th>
<th>Address</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach (var student in Model)
{
<tr>
<td>
@student.CustomerNo
</td>
<td>
@student.FullName
</td>
<td>
@student.Birthday
</td>
<td>
@student.Address
</td>
<td>
@student.Email
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
@model StudentsAcademy.Models.StudentsModel
@{
Layout = null;
}
<html>
<head>
<title>Student Details</title>
</head>
<body>
<div>
CustomerNo : @Model.CustomerNo
</div>
<div>
FullName : @Model.FullName
</div>
<div>
Birthday : @Model.Birthday
</div>
<div>
Address : @Model.Address
</div>
<div>
Email : @Model.Email
</div>
</body>
</html>
https://localhost:44382/Home/Details

@model List<StudentsAcademy.Models.StudentsModel>
@model StudentsAcademy.Models.StudentsModel
StudentsAcademy.Models.StudentsModel

using StudentsAcademy.Models

Details.cshtml
@model StudentsModel
@{
Layout = null;
}
<html>
<head>
<title>Student Details</title>
</head>
<body>
<div>
CustomerNo : @Model.CustomerNo
</div>
<div>
FullName : @Model.FullName
</div>
<div>
Birthday : @Model.Birthday
</div>
<div>
Address : @Model.Address
</div>
<div>
Email : @Model.Email
</div>
</body>
</html>
AllStudents.cshtml
@model List<StudentsModel>
@{
Layout = null;
}
<html>
<head>
<title>Index</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Student No</th>
<th>FullName</th>
<th>Birthday</th>
<th>Address</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach (var student in Model)
{
<tr>
<td>
@student.CustomerNo
</td>
<td>
@student.FullName
</td>
<td>
@student.Birthday
</td>
<td>
@student.Address
</td>
<td>
@student.Email
</td>
</tr>
}
</tbody>
</table>
</body> </html>

اترك تعليقك