Reputation: 2017
I'm getting the value from db and assigning values to Viewbag and passing values from controller to view.
But when i access the items of viewbag/viewdata in view to hide and unhide,it is giving null reference exception.Viewbag/Viewdata is loosing it's data in view after returning view from controller.Because of which i'm not able iterate through the foreach loop of viewbag/viewdata
I'm designing data driven controls in the view.Based on the data from the db i want to hide and unhide controls in MVC View???
Please find the code which i'm using: I'm not able to access the viewbag items in my view. If i'm doing wrong suggest me other ways.
Controller:
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;
}
View:
@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>
Upvotes: 0
Views: 2724
Reputation: 231
What is objGenController? Why are you calling that instead of calling CheckTenantControl directly? Alternately, try this:
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;
}
Upvotes: 0
Reputation: 2648
your items list should be like below in your controller action:
var items=new List<TenantModel>();
In your View
@foreach (var item in ViewBag.TenantControl)
{
<th style="text-align: center;">
if (@item.TenantID== 1)
{
@Html.DisplayName(@item.TenantID);
}
else
{
}
</th>
}
once you added @
for foreach
then you do not need @
for if
Upvotes: 0
Reputation: 863
try with this
@foreach (var item in ViewBag.TenantControl)
{
<tr>
<td style="text-align: center;">
@Html.DisplayFor(modelItem => item.TenantID)
</td>
.......
</tr>
}
Upvotes: 0