I have the following class 'schakeling', generated with EF, representing a database table 'schakeling'. In the database 'id' is the primary key and 'plc_id' is a foreign key.
public partial class schakeling
{
public schakeling()
{
this.verbruik = new HashSet<verbruik>();
}
public int id { get; set; }
public int plc_id { get; set; }
public string var_output { get; set; }
public string omschrijving { get; set; }
public int vermogen { get; set; }
public Nullable<bool> status { get; set; }
public Nullable<byte> hourOn { get; set; }
public Nullable<byte> minuteOn { get; set; }
public Nullable<byte> hourOff { get; set; }
public Nullable<byte> minuteOff { get; set; }
public virtual plc plc { get; set; }
public virtual ICollection<verbruik> verbruik { get; set; }
}
I have a view class
public class SchakelingsListViewModel
{
public IEnumerable<schakeling> List { get; set; }
public PagingInfo PagingInfo { get; set; }//Not relevant for this question
//And some more properties...also not relevant
}
I have the following view (omitted some HTML for brevity)
@model PortalControl.ViewModels.SchakelingsListViewModel
<h2>Index</h2>
<table>
@foreach (var item in Model.List) {
@Html.TableRowFor(item)
}
</table>
I have a generic html helper method TableRowFor because I want to be able to use the method on other domain entities generated with EF. The method generates simple table data.
public static MvcHtmlString TableRowFor<T>(this HtmlHelper helper, T obj)
{
string controller = obj.GetType().BaseType.Name;
string id = obj.GetType().GetProperty("id").GetValue(obj).ToString();
StringBuilder sb = new StringBuilder("<tr>");
sb.Append("<td>");
sb.Append("<a href='" + controller + "/Edit/" + id + "'><img src='/Images/edit-icon.png' /></a>");
sb.Append("<a href='" + controller + "/Details/" + id + "'><img src='/Images/details-icon.png' /></a>");
sb.Append("<a href='" + controller + "/Delete/" + id + "'><img src='/Images/delete-icon.png' /></a>");
sb.Append("</td>");
foreach (var property in obj.GetType().GetProperties())
{
//If statement below filters out the two virtual properties(plc, verbruik) of the schakeling class(as said, generated with EF), somewhat ugly but it works, better suggestions are welcome..
if ((!property.PropertyType.Name.ToLower().Contains("icollection")) && (property.PropertyType.CustomAttributes.Count() != 0))
{
sb.Append("<td>");
//if property is foreign key
//sb.Append("<a href='" + correctControllerNameHere + "/Details/" + property.GetValue(obj) + "'><img src='/Images/details-icon.png' /></a>");
//else
//sb.Append(property.GetValue(obj));
sb.Append("</td>");
}
}
sb.Append("</tr>");
return new MvcHtmlString(sb.ToString());
}
The question I have is, I would like to create a link if a property is a foreign key.
I have searched the internet but I am no expert in PropertyInfo, MetaDataClassType and other similar classes. Something like property.isForeign() would be lovely but anything that works will be appreciated.