[blazor] Building a blazor form - 컴포넌트 Class에 Razor 페이지 url의 파라미터 전달
페이지 정보
작성자 sbLAB 댓글 0건 조회 9,180회 작성일 21-10-20 07:52본문
C# 웹에서 Partial Class (Behind Source)에 익숙한데.. Blazor 는 Razor 페이지에서 클래스를 상속받아서 사용함.
Partial Class
Partial Class (Behind Source) 는 *.Razor.cs 가 Razor 페이지와 묶이니까 IDE툴에서 좋아보이는 점이 있음.
그런데, Razor 페이지에 있는 C# 소스를 Partial Class (Behind Source)로 다 끌고 들어와야하는 부담이 있음.
Razor 페이지에서 C# 소스를 넣는 형태로 한 이유가 있을텐데...
Partial Class (Behind Source) 는 소스만 분리된것일 뿐 Razor 페이지와 한몸이므로, c#소스 변수에 모두 접근가능
ComponentBase
컴포넌트베이스클래스를 만든 후 어떤 원하는 Razor 페이지에서 상속받은 후 사용하면됨(상속개념이니까 protected 캡슐화 가능)
- Razor 페이지를 url 호출할때 .../editemployee/1 처럼 파라미터(1)를 넘겨준다면,
Razor 페이지의 @inherits EditEmployeeBase 실행되고
-> EditEmployeeBase 클래스에서 파라미터를 받아야 하는데..?
[Parameter] <==== Attribute 이용, Id변수에 파라미터로 받겠다고 알려줌. public string Id { get; set; } protected async override Task OnInitializedAsync() { Employee = await EmployeeService.GetEmployee(int.Parse(Id)); }
그런데... 어떻게 Id 변수를 인식한것일까?
이유는 Razor 페이지 최상단 라우터에 @page "/editemployee/{id}" 라고 알려줬기 때문임.
만약 여러개 파라미터를 받아야 한다면.. 아래처럼 됨.
@page "/navigatetopage/{id:int}/{key}"
@code {
[Parameter] public int Id{get;set;}
[Parameter] public string Key{get;set;}
...
}
[선택]
- 고정적인 razor 페이지는 Partial Class 비하인드 소스로 하고.. <- c# 소스만 볼수 있어 독해가 빠름.
- Select Data 콤포넌트 성격이 있어 재사용 가능한 경우 ComponentBase 클래스 만들어서 여러 페이지에서 상속받아 사용.
- 데이타성격이 있는 경우는 ComponentBase 클래스
https://stackoverflow.com/questions/66311798/blazor-componentbase-or-partial-class
https://www.pragimtech.com/blog/blazor/building-blazor-form/
EmployeeDetails.razor
Edit Employee Component Class (EditEmployeeBase.cs)
Redirect to EditEmployee component
- 이전글[blazor] Blazor ComponentBase or Partial Class 21.10.20
- 다음글[.net core] Why use tag helpers 21.10.10
댓글목록
등록된 댓글이 없습니다.