Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Asp.net core mvc 在引导UI元素中设置活动类的更好方法_Asp.net Core Mvc_Asp.net Core 2.0 - Fatal编程技术网

Asp.net core mvc 在引导UI元素中设置活动类的更好方法

Asp.net core mvc 在引导UI元素中设置活动类的更好方法,asp.net-core-mvc,asp.net-core-2.0,Asp.net Core Mvc,Asp.net Core 2.0,我有一个引导按钮组,用于控制筛选: <div class="btn-group btn-group" role="group" aria-label="AM/PM"> <a href="/PrivateHires/StateFair?ampm=am" role="button" class="btn btn-default">AM</a> <a href="/PrivateHires/StateFair?ampm=pm" role="button

我有一个引导按钮组,用于控制筛选:

<div class="btn-group btn-group" role="group" aria-label="AM/PM">
  <a href="/PrivateHires/StateFair?ampm=am" role="button" class="btn btn-default">AM</a>
  <a href="/PrivateHires/StateFair?ampm=pm" role="button" class="btn btn-default">PM</a>
</div>
我创建了一个函数,但无法使用
a
语法:

public string active(string AmPm)
{
    if (ViewData["ampm"].ToString()==AmPm){ return "class='active'"; }
    else { return ""; }
}

...

<a href="/FooController/Foo?ampm=am" role="button" class="btn btn-default @ative('AM')">AM</a>
<a href="/FooController/Foo?ampm=pm" role="button" class="btn btn-default @active('PM')">PM</a>
公共字符串处于活动状态(字符串AmPm)
{
if(ViewData[“ampm”].ToString()==ampm){return“class='active'”;}
else{return”“;}
}
...

有没有更优雅的方法来设置
活动
类?一般来说,有没有更好的方法根据查询字符串值修改UI元素?

我修改了函数:

@functions
{
  public string ActiveAmPm(string ampm)
  {
    if (ViewData["ampm"].ToString() == ampm) { return "active"; }
    else { return ""; }
  }
}
然后引用它:

<div class="btn-group btn-group" role="group" aria-label="AM/PM">
    <a href="/FooController/Foo?ampm=am" role="button" class='btn btn-default @ActiveAmPm("AM")'>AM</a>
    <a href="/FooController/Foo?ampm=pm" role="button" class='btn btn-default @ActiveAmPm("PM")'>PM</a>
</div>


我觉得这种方法听起来很好。您需要只返回
活动的
字符串,因为标记中已有
class=”“
。请使用
ViewContext.RouteData.Values[“ampm”]
而不是
ViewData
。使用
ViewData
取决于在
ViewData
中键入的实际设置,这很容易被忽略或忘记。如果它在查询字符串中传递,它将始终位于
RouteData.Values
。实际上不需要else。。。只需
返回“”
<div class="btn-group btn-group" role="group" aria-label="AM/PM">
    <a href="/FooController/Foo?ampm=am" role="button" class='btn btn-default @ActiveAmPm("AM")'>AM</a>
    <a href="/FooController/Foo?ampm=pm" role="button" class='btn btn-default @ActiveAmPm("PM")'>PM</a>
</div>