[RESOLVED]Setup query, navigation properties (Code First) in view (Entity Framework 6 + MVC 5)

Hi!!! Sorry that I’m posting this here. But seems that no one knows (or have the time) to help me with this one :p

What I’m trying to achieve is quite simple. Through code first, I’d like to allow a user to have a series of contacts. Each of them will have the following options: "None", "No encounter yet", and "Client". What I’m looking for, is to add the option selected
to the database and retrieve its value.

The problem is that I do not know very well how to setup correctly the relationships. Or I do not know if the query I’m executing is actually managing the relationship.

The approach I’m going after is simple: I create a dropdown list (dictionary based) which retrieves the data from "ExposureResult" table (which is the table in which the options I mentioned previously reside: None, No encounter yet, Client), and shows it
to the user at the Contacts creation form.

When submitted, it will give me the ID of the row from the "ExposureResult" table, and I would insert it into the ExposureResultID (which is the property *I think* I have set as a relationship), so I can call it later.

Later on I execute the following query:

var query = from c in dbs.ContactsDB where c.IboID == uid select c;
return query;

Where dbs.ContactsDB is the DB Context of the Contacts model I have implemented and retrieves all the values I’m looking after.

The query variable will return to me the model in an IEnumerable.

Which then, in the .cshtml file I would call it like this:

@foreach (var item in Model) {

item.ExposureResult
}

But it wouldn’t return me the result I’m expecting, which is getting what does the ID value refers in the ExposureResult, and not the ID itself.

Here’s the model

The model is the following:

public class Contacts
{
[HiddenInput(DisplayValue=false)]
[Key]
public int ContactsID { get; set; }
[HiddenInput(DisplayValue=false)]
public int IboID { get; set; }
public string Name { get; set; }

[Display(Name="First Time Met")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public string FirstMeet { get; set; }
public string Phone { get; set; }

[Display(Name="Date of Contact")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? DateOfContact { get; set; }

public string Comments{ get; set; }

[Display(Name="Next Exposure")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? NextExposure { get; set; }

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Next Exposure 2")]
public DateTime? NextExposure2 { get; set; }

//This is analogue to SelectedQ from Ibo Model
//The main purpose is to receive the incoming data from the SelectList
[NotMapped]
public string SelectedResult { set; get; }

[Display(Name = "Result")]
public int ExposureResultID { get; set; }
public virtual ICollection<ExposureResults> ExposureResult {get; set;}
//public virtual ICollection<ExposureResults> ExposureResults { get; set; }
public virtual ICollection<Ibo> Ibo { get; set; }
}

public class ExposureResults
{
public int ID { get; set; }
public string ExposureResult { get; set; }
}

I’m putting on an image to better explain what I’m trying to achieve:
Process

Bump* :p

Hi superjose,

Thanks for your post.

superjose

The problem is that I do not know very well how to setup correctly the relationships. Or I do not know if the query I’m executing is actually managing the relationship.

Please check this article:

#ASP.NET MVC, Entity Framework, Modifying One-to-Many and Many-to-Many Relationships

http://www.mikesdotnetting.com/Article/110/ASP.NET-MVC-Entity-Framework-Modifying-One-to-Many-and-Many-to-Many-Relationships

Maybe it will help you.

BEST Regards,

Eileen

Thanks Eileen :D

I’ll be checking it!!!

Leave a Reply