简体   繁体   中英

How to Show and Hide Div using C# in MVC 3 Razor View Engine?

我必须编写C#代码来显示和隐藏MVC3中的div,用于基于C#中的switch case的各种控件。如何在不使用JQuery Show或隐藏的情况下完成...但是在完全服务器端...?

Add your switch statement directly into your .cshtml file. It will all be server-side at that point.

Controller:

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>

W3c has a Article about Logic Conditions

Use this sample

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

or see 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;                   
    }
}

Why you use switch statement??

Do you like if condition???

for

<% if(CheckYourCondition){ %>

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

<% } %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM