إنشاء واستخدام Partial View
-
Partial Views هي ملفات View منفصلة يمكن ادراج محتوياتها في Views أخرى. مثال - إذا كان لديك view يحتوي على محتويات تتكرر مرارًا وتكرارًا، فيمكنك وضع هذا المحتوى في partial view وتحميل هذه المحتويات من Partial View. بهذه الطريقة يمكنك منع تكرار الكود code duplication.
Partial Views هي ملفات cshtml. عادية ولا تختلف عن regular views إلا في الوظيفة التي تقوم بها.
لنفرض هنا اننا بحاجة الى شاشة لإدخال مبلغ الفاتورة للطلاب، عند كل عملية او شراء. حيث يمكن استخدام هذه الشاشة من داخل نظام الجامعة، من خلال موقع الجامعة، من خلال تطبيق الموبايل، فبدلا من انشاء 3 صفحات لكل منصة (النظام، الموقع، الموبايل) سنقوم بإنشاء صفحة Partial Views، بحيث يتم بناء هذه الصفحة مرة واحدة بالشكل المطلوب، ومن ثم استخدامها في أكثر من مكان.
أضف Controller جديد باسم PaymentController ثم أضف action داخلة باسم CardView
الكود التالي يوضح ذلك:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace StudentsAcademy.Controllers
{
public class PaymentController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult CardView()
{
return View();
}
}
}

@model PaymentModel
@{
Layout = null;
}
<form method="post" asp-action="DoPayment">
<div class="container">
<main role="main" class="pb-3">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>CustomerNo:</label>
<input class="form-control" asp-for="CardHolderName" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Card Number:</label>
<input class="form-control" asp-for="CardNumber" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Card Expiry Date:</label>
<input class="form-control" asp-for="CardExpiryDate" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>CCV:</label>
<input class="form-control" asp-for="SecurityCode" />
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</main>
</div>
</form>
- CardHolderName
- CardNumber
- CardExpiryDate
- SecurityCode
@{
Layout = null;
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace StudentsAcademy.Models
{
public class PaymentModel
{
public string CardNumber { get; set; }
public string Number { get; set; }
public string CardHolderName { get; set; }
public string SecurityCode { get; set; }
public string ExpiryDateYear { get; set; }
public string ExpiryDateMonth { get; set; }
public string CardExpiryDate { get; set; }
public string ExpiryDate { get; set; }
}
}
https://localhost:44382/payment/CardView

@await Html.PartialAsync()
@await Html.PartialAsync("CardView")

@await Html.PartialAsync("~/Views/Payment/CardView.cshtml",Model.Payment)
@model StudentsModel
@{
ViewData["Title"] = "StudentInfo";
}
Welcome ,@ViewBag.UserName
<hr />
<p>Current Date: @Model.CurrentDate</p>
<hr />
<p>Current Year: @Model.CurrentDate.Year</p>
<h2>Student Info</h2>
<p>Customer No : @Model.CustomerNo</p>
<p>Full Name: @Model.FullName</p>
<p>Birthday: @Model.Birthday</p>
<p>Gender : @Model.Gender </p>
<p>Nationality : @Model.Nationality </p>
<p>Address : @Model.Address </p>
<p>UserName : @Model.UserName </p>
<p>Password : @Model.Password</p>
<p>Email : @Model.Email </p>
<p>Mobile : @Model.Mobile </p>
<hr />
<p>Payment Info</p>
@await Html.PartialAsync("~/Views/Payment/CardView.cshtml",Model.Payment)
using 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; }
public DateTime CurrentDate { get; set; }
public PaymentModel Payment { get; set; }
}
}

@await Html.PartialAsync("~/Views/Payment/CardView.cshtml")
@model List<string>
<div style="color:red">
This is partial View:
<ul>
@foreach (string str in Model)
{
<li>@str</li>
}
</ul>
</div>
@await Html.PartialAsync("~/Views/Payment/CardView.cshtml", new List<string> { "Classic ASP", "ASP.NET Web Forms", "ASP.NET MVC", "ASP.NET Core MVC" })
اترك تعليقك