如何在 MVC 3 Razor 视图引擎中使用 C# 显示和隐藏 Div?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15263192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:09:46  来源:igfitidea点击:

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

c#asp.net-mvcrazor

提问by Vignesh Subramanian

I have to write C# code for showing and hiding div in MVC3 for various controls based on switch case in C# .How can it be done without using JQuery Show or hide.. but in fully server side..?

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

采纳答案by Middas

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

将 switch 语句直接添加到 .cshtml 文件中。届时一切都将是服务器端的。

Controller:

控制器:

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

CSHTML:

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>

回答by Middas

W3c has a Article about Logic Conditions

W3c 有一篇关于逻辑条件的文章

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

回答by Middas

Why you use switch statement??

为什么要使用switch语句??

Do you like if condition???

你喜欢如果条件???

for

为了

<% if(CheckYourCondition){ %>

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

<% } %>