c# - MVC: Implementing Generic Controllers and Views. Empty values while enumeration in view issue -
continuing here (thanks @qujck) i've implemented generic controller looks this:
public class genericlookupcontroller<tmodeltype> : controller tmodeltype : genericlookupmodel //genericlookupcontroller.cs
it has common actions handling common tasks in lookup tables crud, toggling active state , such. i'm trying create generic views generic lookup tables. model type genericlookupmodel
looks 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 example country lookup controller , model:
public partial class country : genericlookupmodel //country lookup model { }
and controller:
public class countrycontroller : genericlookupcontroller<country> //countrycontroller.cs { }
actions working fine those, have problem views. index
action i'm using:
public virtual actionresult index() //genericlookupcontroller.cs { var model = _db.set<tmodeltype>().tolist(); return view("__genericlookupindex",model); }
and __genericlookupindex
looks 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 item
doesn't have enumerated values model
in line foreach
. i've tried workaround , instead of @foreach (var item in model)
did @foreach (country item in model)
, works denies whole generic idea. how can fix it, don't have specify type explicitly in view , data properly? please advice.
update: application doesn't throw errors, don't proper values while enumeration. please screenshots below:
1) empty table generated view
2) model variable in watch window
3) enumerated item variable model in watch window
Comments
Post a Comment