Continuing from here (thanks @qujck) I've implemented Generic controller which looks like this:
public class GenericLookupController<TModelType> : Controller where TModelType : GenericLookupModel //GenericLookupController.cs
It has all common actions for handling most common tasks in lookup tables like CRUD, toggling active state and such.
I'm trying now to create also generic views for those generic lookup tables.
Model type is GenericLookupModel which basically looks like this:
public class GenericLookupModel : IActive, CustomField
{
public bool is_active { get; set; }
public string value { get; set; }
public DateTime? created_on { get; set; }
public string created_by { get; set; }
public DateTime? updated_on { get; set; }
public string updated_by { get; set; }
public int id {get;set;}
}
So let's take for example country lookup controller and model:
public partial class Country : GenericLookupModel //country lookup model
{
}
and controller:
public class CountryController : GenericLookupController<Country> //CountryController.cs
{
}
Actions are working fine for those, but I have a problem with views. For Index action I'm using:
public virtual ActionResult Index() //GenericLookupController.cs
{
var model = _db.Set<TModelType>().ToList();
return View("__GenericLookupIndex",model);
}
and the __GenericLookupIndex looks like that:
@model IEnumerable<GenericLookupModel> @*__GenericLookupIndex.cshtml*@
@{
Layout = "_AdminLayout.cshtml";
}
...
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.value)
</td>
<td>
@{
if (item.is_active)
{
<i class="fa fa-check"></i>
}
else
{
<i class="fa fa-times-circle"></i>
}
}
</td>
<td>
@Html.DisplayFor(modelItem => item.created_on)
</td>
<td>
@Html.DisplayFor(modelItem => item.created_by)
</td>
<td>
@Html.DisplayFor(modelItem => item.updated_on)
</td>
<td>
@Html.DisplayFor(modelItem => item.updated_by)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { item.id }, new { @class = "btn btn-primary btn-xs" }) |
@Html.ActionLink("Deactivate/Activate", "Toggle", new { item.id }, new { @class = "btn btn-warning btn-xs" })
</td>
</tr>
}
The issue is that item doesn't have enumerated values from Model in the line with foreach. I've tried to do workaround and instead of @foreach (var item in Model) I did @foreach (Country item in Model) and that works but this denies the whole Generic idea. How can I fix it, so that I don't have to specify the type explicitly in the view and get the data properly? Please advice.
Update: The application doesn't throw any errors, but I don't get proper values while enumeration. Please look into screenshots below:
1) Empty table generated by view
2) Model variable in Watch window
3) Enumerated item variable from Model in Watch window
itemvariable is filled with default values instead of values taken from enumerated Model. So I end up with table filled with blank/default values. I.e. Button for edit navigates to/Edit/0insead of/Edit/10and column with value is blank insead of i.e. Israel I'll provide some screenshots in few minutesCountryclass.Countryclass comes from Entity Framework and I'm using partials to implement genericity. Please take a look on screenshots provided in the update.Countrypartials as well.