0

我在控制器中有一个返回 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
   } 
4

2 回答 2

0

向 Viewbag 添加一个变量来表示类型:

Viewbag.QueryName = Query;

然后在视图中:

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

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

于 2013-05-16T21:59:21.647 回答
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
} 
于 2013-05-16T01:38:47.003 回答