1

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

10
  • 1
    What exactly is the error you're seeing? Is it shown on run-time only? Commented Aug 18, 2014 at 14:54
  • Application doesn't throw any errors. The item variable 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/0 insead of /Edit/10 and column with value is blank insead of i.e. Israel I'll provide some screenshots in few minutes Commented Aug 18, 2014 at 15:05
  • Did you make sure that in Controller/Action the values are actually there? Also, show your full Country class. Commented Aug 18, 2014 at 15:08
  • Yes, values are there when returned in actions, the Country class comes from Entity Framework and I'm using partials to implement genericity. Please take a look on screenshots provided in the update. Commented Aug 18, 2014 at 15:17
  • Please include all Country partials as well. Commented Aug 18, 2014 at 15:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.