Saturday 15 February 2014

c# - Setup query, navigation properties (Code First) in view (Entity Framework 6 + MVC 5) -



c# - Setup query, navigation properties (Code First) in view (Entity Framework 6 + MVC 5) -

what i'm trying accomplish quite simple. through code first, i'd allow user have series of contacts. each of them have next options: "none", "no encounter yet", , "client". i'm looking for, add together alternative selected database , retrieve value.

the problem not know how setup correctly relationships. or not know if query i'm executing managing relationship.

the approach i'm going after simple: create dropdown list (dictionary based) retrieves info "exposureresult" table (which table in options mentioned reside: none, no encounter yet, client), , shows user @ contacts creation form.

when submitted, give me id of row "exposureresult" table, , insert exposureresultid (which property i think have set relationship), can phone call later.

later on execute next query:

var query = c in dbs.contactsdb c.iboid == uid select c; homecoming query;

where dbs.contactsdb db context of contacts model have implemented , retrieves values i'm looking after.

the query variable homecoming me model in ienumerable.

which then, in .cshtml file phone call this:

@foreach (var item in model) {

item.exposureresult }

but wouldn't homecoming me result i'm expecting, getting id value refers in exposureresult, , not id itself.

here's model

the model 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 analogue selectedq ibo model //the main purpose receive incoming info 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 image improve explain i'm trying achieve:

if mean, need include "exposureresult" properties query , add together foreign key property exposureresults class.

//include dbs.contactsdb.include("exposureresult") or dbs.contactsdb.include(er => er.exposureresult) public class exposureresults { public int id { get; set; } public string exposureresult { get; set; } //foreign key property public int contactsid }

see article more information.

i hope helps!

c# asp.net-mvc entity-framework asp.net-mvc-5

No comments:

Post a Comment