Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设计a<;部门>;根据用户输入mvc5进行标记_C#_Html_Css_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 如何设计a<;部门>;根据用户输入mvc5进行标记

C# 如何设计a<;部门>;根据用户输入mvc5进行标记,c#,html,css,asp.net,asp.net-mvc,C#,Html,Css,Asp.net,Asp.net Mvc,我想根据用户选择的主题来设计我的jumbotron。我想使用if语句,但我不知道在CSHTML中使用if语句的正确方法 我知道下面的代码是不正确的,有人能告诉我怎么做吗 这是我的密码: themes = db.Themes.ToList(); foreach (var item in themes) { if (item.themeImage != null) { string imageBase = Convert.ToBase64String(item.th

我想根据用户选择的主题来设计我的jumbotron。我想使用if语句,但我不知道在CSHTML中使用if语句的正确方法

我知道下面的代码是不正确的,有人能告诉我怎么做吗

这是我的密码:

themes = db.Themes.ToList();

foreach (var item in themes)
{
    if (item.themeImage != null)
    {
        string imageBase = Convert.ToBase64String(item.themeImage);
        imageSource1 = string.Format("data:image/gif;base64,{0}", imageBase);
    }

    if (theme == beach && item.themeID == 1)
    {
        <text>
            <div class="jumbotron" id="@item.themeID" img src="@item.themeImage">
        </text>
    }
    else if (theme == animals && item.themeID == 2)
    {
        <text>
            <div class="jumbotron" id="@item.themeID" style="background-image:@imageSource1; background-size:cover">
        </text>
    }
    else if (theme == kitchen && item.themeID == 3)
    {
        <text>
            <div class="jumbotron" id="@item.themeID" style="background-image:@imageSource1; background-size:cover">
        </text>
    }
}
themes=db.themes.ToList();
foreach(主题中的变量项)
{
if(item.themeImage!=null)
{
字符串imageBase=Convert.tobase64字符串(item.themeImage);
imageSource1=string.Format(“数据:image/gif;base64,{0}”,imageBase);
}
如果(theme==beach&&item.themeID==1)
{
}
else if(theme==动物&&item.themeID==2)
{
}
else if(theme==kitchen&&item.themeID==3)
{
}
}

jumbotron的主题和样式应取决于用户的选择

您可以创建一个小函数并调用它以获得正确的样式

您可以在cshtml中实现这一点

@{
    string GetStylePerTheme(int themeId)
    {
        switch(themeId)
        {
            case 1:
                return "color: red;";
            case 2:
                return "color: blue;";
            default:
                return "color: black;";
        }
    }
}
并将其用于html(与cshtml相同)


现在,如果您想让它在多个页面之间保持干净和可重用,请创建一个自定义帮助器(实际上是一个简单的静态方法)

公共静态字符串GetStylePerTheme(此HtmlHelper帮助程序,int-themeId) { 开关(themeId) { 案例1: 返回“颜色:红色;”; 案例2: 返回“颜色:蓝色;”; 违约: 返回“颜色:黑色;”; } } 而对于cshtml

<text>
    <div class="jumbotron" id="@item.themeID" style="@Html.GetStylePerTheme(item.themeID)">
</text>


扩展HtmlHelper是可选的。

如果您愿意使用JQuery,可以使用$(“#selector”).addClass()。为了减少比较开销,我会为theme:example
If(theme.ThemeType==Themes.ThemeTypes.beach){\\Do Work}
这样比较的值要小得多。对于你的问题,如果你能做到的话,我建议使用jQuery及其addClass函数。否则,您可以在cshtml文件中插入带有“@”符号的c#逻辑。还考虑使用HTML、ReDeRealAcon()或HTML.部分()来简化和模块化。 public static string GetStylePerTheme(this HtmlHelper helper, int themeId) { switch(themeId) { case 1: return "color: red;"; case 2: return "color: blue;"; default: return "color: black;"; } }
<text>
    <div class="jumbotron" id="@item.themeID" style="@Html.GetStylePerTheme(item.themeID)">
</text>