Asp.net mvc 使用服务器代码更改所选标记的颜色

Asp.net mvc 使用服务器代码更改所选标记的颜色,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我有一些a标签 <ul class="" data-persist="true" > @foreach (var item in Model) { <li> <a class="tagactive" href="~/Product/ProductExplain?IdGroup=@item.id_tblgroup&flag=2&idproduct=@item.id">@item.explain </a> <

我有一些
a
标签

<ul class="" data-persist="true" >
@foreach (var item in Model)
{
   <li>
     <a class="tagactive" href="~/Product/ProductExplain?IdGroup=@item.id_tblgroup&flag=2&idproduct=@item.id">@item.explain </a>
   </li>
}
</ul>
试试这个

您可以在
model
中将属性设置为
SelectedTag
。在控制器端设置此属性的值,根据此值,您可以如下设置类

<ul class="" data-persist="true" >
@foreach (var item in Model)
{
   <li>
     <a class="@if (Model.SelectedTag== Item.Name) {<text>tagactive</text>}
               else {<text>taginactive</text>}"
     href="~/Product/ProductExplain?IdGroup=@item.id_tblgroup&flag=2&idproduct=@item.id">@item.explain </a>
   </li>
}
</ul>
    @foreach(模型中的var项目) {
  • }
您可以试试JQuery

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        .MyRed {
        color: red
        }
    </style>
</head>
    <body>
        <div id="myId"></div>

       <ul>
           <li>
               <a href="#" class="MyRed">google.com </a></br>
               <a href="#">test.com </a>
           </li>
        </ul>
        <script src="Scripts/jquery-2.0.3.min.js"></script>
        <script>
            $(document).ready(function () {
                $("a").click(function()
                {
                    $("a").removeClass("MyRed");
                    $(this).addClass("MyRed");
                });

                //  $("a").removeClass
            });
        </script>
    </body>
</html>

MyRed先生{
颜色:红色
}

$(文档).ready(函数(){ $(“a”)。单击(函数() { $(“a”).removeClass(“MyRed”); $(此).addClass(“MyRed”); }); //$(“a”).removeClass });
视图模型中需要其他数据。只需添加一个属性
IsActive
,并在需要更改颜色时将其设置为true。然后你可以在你的
foreach
循环中检查它。@Tanner,请不要把新手送到学校。那个网站被认为是有害的。他说他正在发布这个页面!如何在视图中填充Model.SelectedTag?我说过您需要将值从控制器传递到视图
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        .MyRed {
        color: red
        }
    </style>
</head>
    <body>
        <div id="myId"></div>

       <ul>
           <li>
               <a href="#" class="MyRed">google.com </a></br>
               <a href="#">test.com </a>
           </li>
        </ul>
        <script src="Scripts/jquery-2.0.3.min.js"></script>
        <script>
            $(document).ready(function () {
                $("a").click(function()
                {
                    $("a").removeClass("MyRed");
                    $(this).addClass("MyRed");
                });

                //  $("a").removeClass
            });
        </script>
    </body>
</html>