检查剃刀中的收入视图包的正确语法是什么

时间:2013-05-16 00:48:51

标签: asp.net-mvc asp.net-mvc-3 razor if-statement

我在控制器中有一个动作,返回3个视图。 是否可以检查剃刀控制器发送的Viewbag。 ?

这是我的控制器

     public ActionResult Details(string Query, long Id)
      {
    // some code......
        switch (Query)
        {

            case ("Operationer"):
                ViewBag.Query =Operationer;
               break;

            case ("Material"):
                ViewBag.Query = Material;
                break;

           Case("items"):
                 Viewbag.Query= items;
                   break;
        }

         return view();
          }

在我的剃刀视图中

 @{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/LayoutDetails.cshtml";
}

<h2>Details</h2>

 @ if ( the incom Viewbag == "material")
   {  
 Create table for material 
   }
  @ else if ( the income Viewbaf == "items")
  { 
 create table for items
   } 

2 个答案:

答案 0 :(得分:0)

尝试以下

C#

public ActionResult Details(string Query, long Id)
  {
// some code......
    switch (Query)
    {

        case ("Operationer"):
            ViewBag.Query ="Operationer";
           break;

        case ("Material"):
            ViewBag.Query = "Material";
            break;

       Case("items"):
             Viewbag.Query= "items";
               break;
    }

     return view();
      }

查看

 @{
   ViewBag.Title = "Details";
   Layout = "~/Views/Shared/LayoutDetails.cshtml";
 }

 <h2>Details</h2>

 @ if (ViewBag.Query == "Material")
  {  
     Create table for material 
  }
else if ( ViewBag.Query == "items")
{ 
  create table for items
} 

答案 1 :(得分:0)

向Viewbag添加变量以表示类型:

Viewbag.QueryName = Query;

然后在视图中:

@if(Viewbag.QueryName  == "materials)
{  // etc....
}

...那么,你为什么要用Viewbag而不是视图模型来做这一切?