asp.net mvc 4 - Populate ComboBox MVC4 Entity Framework Database -
i trying populate combobox database. using mvc4 , entity framework. way code sits, combobox filled nova.models.clients. want actual company name filled in.
here model:
public class clients { [key] public int clientid { get; set; } public string companyname { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string phone { get; set; } public string email { get; set; } public string address { get; set; } public string city { get; set; } public string state { get; set; } public string zip { get; set; } public icollection<estimates> estimates { get; set; } public icollection<contracts> contracts { get; set; } }
here controller:
public actionresult editclients() { return view(); }
here view:
@using nova.models @model nova.models.clients @{ var context = new novadb(); var clients = context.clients.orderby((x => x.companyname)); } div class="control-group"> @html.labelfor(x => x.companyname, new { @class = "control-label" }) <div class="controls"> @html.dropdownlistfor(x => x.companyname, new selectlist(clients), new { @class = "input-xlarge" }) </div> </div>
with being first real mvc project, haven't run across before. after speaking buddy bit confused. gathered should put data access in controller , not view, regardless, should still work.
any appreciated.
i recommend using separate view model (not same domain model, , not same entity framework poco object). creates separation of concerns. said, can have following on view model:
public class clientviewmodel { public int clientid { get; set; } public string companyname { get; set; } public ienumerable<string> companynames { get; set; } // other properties here, include needed view }
your controller populate list of possible company names companynames
property on view model (along copying other needed view model properties domain model view model), , list used possible values dropdown. companyname
property used store selected value.
and dropdown in view:
@html.dropdownlistfor(x => x.companyname, new selectlist(model.companynames), new { @class = "input-xlarge" });
like said, make sure data access in controller (if not separate service layer).
Comments
Post a Comment