ویوهای داینامیک برای روابط یک به یک در EF Core

در آموزش ویوهای روابط یک به یک در EF Core، یاد می‌گیریم چگونه داده‌های مرتبط در یک رابطه یک به یک را در View نمایش دهیم. ابتدا از ViewModel برای تجمیع داده‌ها استفاده کرده و سپس در یک View در ASP.NET Core MVC اطلاعات را نمایش خواهیم داد. همچنین، نحوه ایجاد کنترلر های مرتبط را نیز برسی میکنیم.

کنترلر – Controller دانشجو Student

کنترلر StudentController در ASP.NET Core MVC مسئول مدیریت عملیات مرتبط با دانشجویان است. این کنترلر از یک سرویس به نام IStudentService استفاده می‌کند. که عملیات CRUD را روی داده‌های دانشجویان انجام می‌دهد.

وابستگی به سرویس دانشجویان : در این کنترلر، از طریق تزریق وابستگی (Dependency Injection)، یک نمونه از IStudentService دریافت و در متدهای مختلف برای انجام عملیات روی داده‌های دانشجویان استفاده می‌شود. این کار باعث جدا شدن لایه کنترلی (Controller) از لایه سرویس (Service) میشود. و کدها را خواناتر و قابل نگهداری‌تر می‌کند.

private readonly IStudentService _studentService;
public StudentController(IStudentService studentService)
{
    _studentService = studentService;
}
public async Task<IActionResult> Index()
{
    return View(await _studentService.GetAllStudentsAsync());
}
public async Task<IActionResult> Details(int? id)
{
    if (id == null) return NotFound();
    return View(await _studentService.GetStudentByIdAsync(id.Value));
}
public IActionResult Create()
{
    return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,Family,Email")] StudentViewModel student)
{
    if (ModelState.IsValid)
    {
        await _studentService.CreateStudentAsync(student);
        return RedirectToAction(nameof(Index));
    }
    return View(student);
}
public async Task<IActionResult> Edit(int? id)
{
    if (id == null) return NotFound();
    return View(await _studentService.GetStudentByIdAsync(id.Value));
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Family,Email")] StudentViewModel student)
{
    if (id != student.Id) return NotFound();
    if (ModelState.IsValid)
    {
        await _studentService.UpdateStudentAsync(student);
        return RedirectToAction(nameof(Index));
    }
    return View(student);
}
public async Task<IActionResult> Delete(int? id)
{
    if (id == null) return NotFound();
    return View(await _studentService.GetStudentByIdAsync(id.Value));
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
    await _studentService.DeleteStudentAsync(id);
    return RedirectToAction(nameof(Index));
}

کنترلر – Controller کارت دانشجویی StudentCard

کنترلر StudentCardController در ASP.NET Core MVC مسئول مدیریت عملیات مرتبط با کارت دانشجویان است. این کنترلر از یک سرویس به نام IStudentCardService استفاده می‌کند. که عملیات CRUD را روی داده‌های کارت دانشجویان انجام می‌دهد.

وابستگی به سرویس کارت دانشجویان : در این کنترلر، از طریق تزریق وابستگی (Dependency Injection)، یک نمونه از IStudentCardService دریافت و در متدهای مختلف برای انجام عملیات روی داده‌های کارت دانشجویان استفاده می‌شود. این کار باعث جدا شدن لایه کنترلی (Controller) از لایه سرویس (Service) میشود. و کدها را خواناتر و قابل نگهداری‌تر می‌کند.

private readonly IStudentCardService _CardService;
private readonly IStudentService _studentService;
public StudentCardsController(IStudentCardService CardService,
    IStudentService studentService)
{
    _CardService = CardService;
    _studentService = studentService;
}
public async Task<IActionResult> Details(int? id)
{
    if (id == null) return NotFound();
    return View(await _CardService.GetCardByIdAsync(id.Value));
}
public async Task<IActionResult> Create(int? StudentId)
{
    if (StudentId == null) return BadRequest();
    var CheckStudentId = await _studentService.GetStudentByIdAsync(StudentId.Value);
    return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,NumberCard,ExpirationDate,StudentId")] StudentCardViewModel studentCard)
{
    if (ModelState.IsValid)
    {
        await _CardService.CreateCardAsync(studentCard);
        return RedirectToAction(nameof(Index),"Students");
    }
    return View(studentCard);
}
public async Task<IActionResult> Edit(int? id)
{
    if (id == null) return NotFound();
    return View(await _CardService.GetCardByIdAsync(id.Value));
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,NumberCard,ExpirationDate,StudentId")] StudentCardViewModel studentCard)
{
    if (id != studentCard.Id) return NotFound();
    if (ModelState.IsValid)
    {
        await _CardService.UpdateCardAsync(studentCard);
        return RedirectToAction(nameof(Index),"Students");
    }
    return View(studentCard);
}
public async Task<IActionResult> Delete(int? id)
{
    if (id == null) return NotFound();
    return View(await _CardService.GetCardByIdAsync(id.Value));
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
    await _CardService.DeleteCardAsync(id);
    return RedirectToAction(nameof(Index), "Students");
}

مشاهده در آپارات یا یوتیوب

محتوای ویدیویی این صفحه در سایت قابل مشاهده است. اما اگر عادت دارید توی پلتفرم آپارات یا یوتیوب ویدیوها را مشاهده کنید، می‌توانید از دکمه‌های زیر استفاده کنید.

تماشای ویدیوی ویوهای داینامیک برای روابط یک به یک در EF Core در آپارات و یوتیوب

اشتراک‌گذاری با یک کلیک

با یک کلیک، محتوای این صفحه را در شبکه‌های اجتماعی، ایمیل یا پیام‌رسان‌های مورد علاقه‌تان به‌سرعت به اشتراک بگذارید.

اشتراک‌گذاری صفحه ویوهای داینامیک برای روابط یک به یک در EF Core در شبکه‌های اجتماعی

نظرات ارزشمند شما

نظرات شما برای ما ارزشمند است و آنها را میخوانیم و پاسخ میدهیم.

تصویر کریمی

سلام کدupdate-database مربوط به مایگریشن رو که نوشتم این خطا رو میده لطف میکنید راهنمایی کنید ممنون
The name "GETDATE" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

فرم افزودن دیدگاه

با ارسال نظرات خود ما را در ایجاد محتوای بهتر کمک کنید.

با خدمات تهران آی تی آشنا شوید

تهران آی تی با افتخار خدمات زیر را ارائه می‌دهد.