2013-03-07 7 views

Odpowiedz

11

Dodaj instrukcję przełącznika bezpośrednio do pliku .cshtml. W tym momencie wszystko będzie po stronie serwera.

Kontroler:

public ActionResult Page() 
{ 
    string data = "value1"; 
    return View(data); 
} 

CSHTML:

@model string; // this should be the Type your controller passes 

<div>some html content</div> 
@switch(Model) // Model is how you access your passed data 
{ 
    case "value1": 
     <div>...</div> 
    break; 
    case "value2": 
     <div>...</div> 
    break; 
} 
<div>more html content</div> 
+0

thnks, ale jak mogę uzyskać wartość, która jest w kontroler, aby zobaczyć .. –

+0

będę zrewidować mój post ci pokazać. – Middas

+1

@vignesh, Można również użyć 'ViewBag' lub' ViewData' do przekazania wartości z kontrolera do wyświetlenia. –

0

W3C Artykuł o Logic Conditions

skorzystać z tej próbki

@switch(value) 
{ 
    case "YourFistCase": 
     <div>Login</div>; 
    break; 
    case "YourSecondeCase": 
     <div>Logout</div>; 
    break; 
} 

lub zobaczyć sample

// 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;     
    } 
} 
+0

Czy jest możliwe przeniesienie całego tego kodu do kontrolera ... niż w cshtml inline –

-2

Dlaczego używasz instrukcji switch ??

Czy podoba Ci się stan ???

dla

<% if(CheckYourCondition){ %> 

    <div class="TestClass"> 
    Test 
    </div> 

<% } %> 
+2

Uważam, że OP poprosił o rozwiązanie za pomocą Razor ... – jebar8

Powiązane problemy