无法使用Razor(CSHTML)访问MVC中的Viewbag项目?

时间:2014-03-04 08:28:32

标签: jquery .net asp.net-mvc html5 razor

我从db获取值并将值赋给Viewbag并将值从控制器传递到视图。

但是当我访问viewbag / viewdata的项目以隐藏和取消隐藏时,它会给出null引用异常。在从控制器返回视图后,Viewbag / Viewdata在视图中丢失了它的数据。因为我是我无法遍历viewbag / viewdata的foreach循环

我正在视图中设计数据驱动的控件。基于db的数据我想在MVC View中隐藏和取消隐藏控件???

请找到我正在使用的代码:我无法访问我视图中的viewbag项目。如果我做错了,请告诉我其他方法。

控制器:

public ActionResult Index()
{
     objGenController.CheckTenantControl();
     var result = GetTenantData();
     return View(result);
}


public IEnumerable<TenantModel> GetTenantData()
{
     return (from t in db.Tenant.AsEnumerable()
             join c in db.Country.AsEnumerable() on t.CountryID equals c.CountryID
             join s in db.State.AsEnumerable() on t.StateID equals s.StateID
             join ct in db.City.AsEnumerable() on t.CityID equals ct.CityID
             orderby t.Name
             select new TenantModel()
             {
                  TenantID = t.TenantID,
                  Name = t.Name,
                  Address1 = t.Address1,
                  Address2 = t.Address2,
                  Address3 = t.Address3,
                  CountryID = t.CountryID,
                  StateID = t.StateID,
                  CityID = t.CityID,
                  CountryModel = c,
                  StateModel = s,
                  CityModel = ct,
             });
}

public void CheckTenantControl()
{
     var result = GetTenantControlData();
     List<string> items = new List<string>();
     foreach (var item in result)
     {
          items.Add(item.tc.ControlID.ToString());
     }
     ViewBag.TenantControl = items;
}

public IEnumerable<dynamic> GetTenantControlData()
{
     var result = (from tc in dbEntity.TenantControls.AsEnumerable()
                   where tc.TenantID == 1
                   select new { tc });

     return result;
}

查看:

@model IEnumerable<Master.TenantModel>

@{
     ViewBag.Title = "Index";
}

<h2>Tenant</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table style="border-collapse: separate; border-spacing: 15px;">
   <tr>
      @foreach (var item in ViewBag.TenantControl)
      {
          <th style="text-align: center;">
            @if (@item.TenantID== 1)
            {
               @Html.DisplayNameFor(model => model.TenantID);
            }
            else{}
          </th>    
          <th style="text-align: center;">
            @if (@item.Name == 2)
            {
               @Html.DisplayNameFor(model => model.Name)
            }
            else{}
          </th>
          <th style="text-align: center;">
            @if (@item.Address1 == 3)
            {
               @Html.DisplayNameFor(model => model.Address1)
            }
            else{}
         </th>
      //Like this i want to hide and unhide
      }
   </tr>
   </tr>
   @foreach (var item in Model)
   {
        <tr>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.TenantID)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.Address1)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.Address2)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.Address3)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.CountryModel.Name)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.StateModel.Name)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.CityModel.Name)
            </td>
            <td style="text-align: center;">
                @Html.DisplayFor(modelItem => item.Pin)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Phone)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.TenantID }) |
                @Html.ActionLink("Details", "Details", new { id = item.TenantID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.TenantID })
            </td>
        </tr>
   }
</table>

3 个答案:

答案 0 :(得分:0)

您的项目列表应如下所示:您的控制器操作:

 var items=new List<TenantModel>(); 

在您的视图中

@foreach (var item in ViewBag.TenantControl)
{
    <th style="text-align: center;">
        if (@item.TenantID== 1)
        {
            @Html.DisplayName(@item.TenantID);
        }
        else
        {

        }
    </th>
}

@添加foreach后,您@不需要if

答案 1 :(得分:0)

试试这个

@foreach (var item in ViewBag.TenantControl)
{
    <tr>
        <td style="text-align: center;">
            @Html.DisplayFor(modelItem => item.TenantID)
        </td>
        .......
    </tr>
}

答案 2 :(得分:0)

什么是objGenController?你为什么要调用它而不是直接调用CheckTenantControl?或者,试试这个:

public ActionResult Index()
{
     ViewBag.TenantControl = objGenController.CheckTenantControl();
     var result = GetTenantData();
     return View(result);
}

public List<string> CheckTenantControl()
{
     var result = GetTenantControlData();
     List<string> items = new List<string>();
     foreach (var item in result)
     {
          items.Add(item.tc.ControlID.ToString());
     }
     return items;
}