c# - Linq query not returning values -


i have 2 dbcontexts, not sure if makes difference. applicationdbcontext , companydbcontext. tables have been setup within database , have added data within companydetails table.

i have couple problems code, not sure have done within view regarding companyid correct, question not sure why linq query not return results when have data in table.

my companydbcontext goes is

public class companydbcontext : dbcontext {     public dbset<companydetails> companies { get; set; } } 

and here model

public class companydetails {     [key, databasegenerated(databasegeneratedoption.identity)]     public int? companyid { get; set; }      [display(name = "company name")]     public string companyname { get; set; } } 

i have written linq query should have got 1 row put the database. linq query construct list of selectlistitems pass viewbag, , want able have dropdown box company name in, in user selects.

 public actionresult register()     {         //var regviewmodel = new registerviewmodel();          list<selectlistitem> companylist = null;          using(var companies = new companydbcontext())         {             var companynamesquery = (from comp in companies.companies                                      select new selectlistitem                                      {                                          text = comp.companyname,                                          value = comp.companyid.tostring()                                      });             companylist = companynamesquery.tolist();              @viewbag.companies = companylist;         }          return view();     } 

and here view,

    @using (html.beginform("register", "account", formmethod.post, new { @class = "form-horizontal", role = "form" })) {     @html.antiforgerytoken()     <h4>create new account.</h4>     <hr />     @html.validationsummary("", new { @class = "text-danger" })     <div class="form-group">         @html.labelfor(m => m.email, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.textboxfor(m => m.email, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.usercompanyid, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.dropdownlistfor(m => m.usercompanyid, new selectlist(viewbag.companies, "companyid", "companyname"))         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.password, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.passwordfor(m => m.password, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.confirmpassword, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.passwordfor(m => m.confirmpassword, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         <div class="col-md-offset-2 col-md-10">             <input type="submit" class="btn btn-default" value="register" />         </div>     </div> } 

here connection strings

    <add name="defaultconnection" connectionstring="data source=xxxxxx\sqlexpress;initial catalog=dbtest;user id=x;password=xxxxx1;persist security info=false;" providername="system.data.sqlclient" /> <add name="devconnection" connectionstring="data source=xxxxxx\sqlexpress;initial catalog=dbtest;user id=x;password=xxxxx1;persist security info=false;" providername="system.data.sqlclient" /> 

per comments, double check connection string. either you're pointing wrong server, wrong database, or data never made table.

this line:

@html.dropdownlistfor(m => m.usercompanyid, new selectlist(viewbag.companies, "companyid", "companyname")) 

in line:

var companynamesquery = (from comp in companies.companies                                  select new selectlistitem                                  {                                      text = comp.companyname,                                      value = comp.companyid.tostring()                                  }); 

you created new list of selectlistitem. properties text , value available on view.

this dropdown list should be:

@html.dropdownlistfor(m => m.usercompanyid, new selectlist(viewbag.companies, "value", "text")) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -