CSHTML 페이지에서 switch 문 구현
나는 뭔가 다른 것을 시도하고있다. Id가 포함 된보기가 있습니다. Id의 값에 따라 표시되는 제목을 변경하고 싶습니다. 다음과 같은 것 :
@{ switch id
case "test": @;<h1>Test Site</h1>
case "prod": @:<h1>Prod Site</h1>
break;
}
케이스 조건이 꽤 많아서 케이스 사용이 가장 좋겠 지만요. 누구든지 내가 이것을 어떻게 할 수 있는지 제안하고 그것을 작동시킬 수 있습니까? 구문 오류가 많이 발생하므로 코드가 잘 작성되지 않은 것 같습니다.
스위치는 블록으로 완전히 둘러싸여 있어야하며 적절하게 "파손"되어야합니다.
// Use the @{ } block and put all of your code in it
@{
switch(id)
{
case "test":
// Use the text block below to separate html elements from code
<text>
<h1>Test Site</h1>
</text>
break; // Always break each case
case "prod":
<text>
<h1>Prod Site</h1>
</text>
break;
default:
<text>
<h1>WTF Site</h1>
</text>
break;
}
}
<h1>태그 자체가 html 블록으로 묶여 있기 때문에 <text>분리를 위해 블록이 필요하지 않을 수 있습니다 . 그것들을 포함시키는 것은 단지 내 습관입니다.
@switch (id)
{
case "test": <h1>Test Site</h1>
break;
case "prod": <h1>Prod Site</h1>
break;
}
전체 switch 문을 @{}블록 으로 묶을 필요가 없습니다 (Joel Etherton의 게시물과 달리).
Your errors are basically regular syntax errors and have nothing to do with razor;
the variable wasn't in parenthesis
the body of switch wasn't in brackets
no "break" after the first statement.
This doesn't answer your question, as indicated by the question's title, but it does solve the problem you described in the body of the question.
Use a view model class as the view's model and add a method that includes the switch statement. Then just call the method from the view via @Model.MethodWithSwitchStatement(). [The id can be saved in the view model object.]
ReferenceURL : https://stackoverflow.com/questions/7347989/implementing-a-switch-statement-in-a-cshtml-page
'Program Club' 카테고리의 다른 글
| Gradle-Java 플러그인의 jar 파일 이름 (0) | 2021.01.09 |
|---|---|
| 작업을 취소하면 예외가 발생합니다. (0) | 2021.01.09 |
| 인용 부호 안에 인용 부호 사용 (0) | 2021.01.09 |
| Salesforce 인증 실패 (0) | 2021.01.09 |
| ImageButton에 Ontouch와 Onclick을 모두 사용하는 방법은 무엇입니까? (0) | 2021.01.09 |