Administrator paneli: kataloglarni boshqarish


Download 271.02 Kb.
bet3/6
Sana25.01.2023
Hajmi271.02 Kb.
#1118777
1   2   3   4   5   6
Bog'liq
Administrator paneli kataloglarni boshqarish

CRUD usullarini yaratish
ListView boshqaruvi Game ma'lumotlar ob'ektlarini qanday olish va saqlashni bilmaydi, shuning uchun biz ombor bilan o'zaro aloqada bo'lgan kod sinfida bir qator usullarni amalga oshirishimiz kerak. Quyidagi misolda \ Pages \ Admin \ Games.aspx.cs faylining mazmuni ko'rsatilgan:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GameStore.Models;
using GameStore.Models.Repository;
using System.Web.ModelBinding;

namespace GameStore.Pages.Admin


{
public partial class Games : System.Web.UI.Page
{
private Repository repository = new Repository();

protected void Page_Load(object sender, EventArgs e)


{

}


public IEnumerable GetGames()
{
return repository.Games;
}

public void UpdateGame(int GameID)


{
Game myGame = repository.Games
.Where(p => p.GameId == GameID).FirstOrDefault();
if (myGame != null && TryUpdateModel(myGame,
new FormValueProvider(ModelBindingExecutionContext)))
{
repository.SaveGame(myGame);
}
}

public void DeleteGame(int GameID)


{
Game myGame = repository.Games
.Where(p => p.GameId == GameID).FirstOrDefault();
if (myGame != null)
{
repository.DeleteGame(myGame);
}
}

public void InsertGame()


{
Game myGame = new Game();
if (TryUpdateModel(myGame,
new FormValueProvider(ModelBindingExecutionContext)))
{
repository.SaveGame(myGame);
}
}
}
}
Ushbu usullarning CRUD operatsiyalariga qanday mos kelishini ko'rish oson. GetGames () usuli ma'lumotlar moslamalarini o'qiydi. InsertGame (), UpdateGame () va DeleteGame () usullari ma'lumotlar moslamalarini yaratish, yangilash va yo'q qilish uchun javobgardir.
Biz ushbu usullarni ListView boshqaruviga Web-formatda atributlar ketma-ketligi yordamida etkazamiz:
HTML
...
SelectMethod="GetGames"
UpdateMethod="UpdateGame"
DeleteMethod="DeleteGame"
InsertMethod="InsertGame"
...>
...
ListView nazorati brauzer talab qiladigan HTML va JavaScript kodlarini yaratishda g'amxo'rlik qiladi va foydalanuvchi harakatlariga javoban bizning CRUD usullarimiz chaqirilganligiga ishonch hosil qiladi. Shaxsiy so'rovlar, shakl ma'lumotlarini tahlil qilish va ushbu jarayonning boshqa jihatlari bilan shug'ullanishimiz shart emas.

Download 271.02 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling