c# - Loading a page with values from multiple tables using a ViewModel -
what want have number of labels populate information 2 different tables. when submit button pressed, hit post method , save info user typed in , info automatically populated db, i'm having issues method. method in controller looks this:
[httpget] public actionresult addorganization(int peopleid = 1) { var peoplemodel = db.people.include("employeecontacts").single(g => g.peopleid == peopleid); return view("../setup/addorganization", peoplemodel); }
however, i've got viewmodel looks , includes tables needed , post methods page:
public class addorganizationviewmodel { public musicstore.models.organizations organizations { get; set; } public musicstore.models.people people { get; set; } public musicstore.models.organizationoptions organizationsoptions { get; set; } public musicstore.models.employeecontacts employeecontacts { get; set; } }
so when view first loaded, , method above called, error saying
the model item passed dictionary of type 'system.data.entity.dynamicproxies.people_8080c33752a42a0c994331f5015bccfceb99b3ecd7ab8ca2bb11abe67851b81b', dictionary requires model item of type 'musicstore.viewmodels.addorganizationviewmodel'.
here view also:
<h2>addorganization</h2> @model musicstore.viewmodels.addorganizationviewmodel @using (html.beginform("add", "addorganization")) { <fieldset> <legend>contact information</legend> <div class ="editor-label"> @html.label("organization name") </div> <div class ="editor-field"> @html.textboxfor(model => model.organizations.orgname) </div> <div class ="editor-label"> @html.label("phone number") </div> <div class ="editor-field"> @html.textboxfor(model => model.organizations.orgphone) </div> <div class ="editor-label"> @html.label("point of contact") </div> <div class ="editor-field"> @html.textboxfor(model => model.organizations.orgpointofcontact) </div> <div class ="editor-label"> @html.label("office location") </div> <div class ="editor-field"> @html.textboxfor(model => model.organizations.orgofficelocation) </div> </fieldset> <fieldset> <legend>first employee of organization</legend> <div class ="editor-label"> @html.label("admin first name") </div> <div class ="editor-field"> @html.textboxfor(model => model.people.firstname) </div> <div class ="editor-label"> @html.label("admin last name") </div> <div class ="editor-field"> @html.textboxfor(model => model.people.lastname) </div> <div class ="editor-label"> @html.label("admin nid") </div> <div class ="editor-field"> @html.textboxfor(model => model.people.nid) </div> <div class ="editor-label"> @html.label("admin sid") </div> <div class ="editor-field"> @html.textboxfor(model => model.people.sid) </div> <div class ="editor-label"> @html.label("admin email") </div> <div class ="editor-field"> @html.textboxfor(model => model.employeecontacts.email) </div> <div class ="editor-label"> @html.label("admin phone number") </div> <div class ="editor-field"> @html.textboxfor(model => model.employeecontacts.primaryphone) </div>
how send needed information view via method when can take viewmodel type? there way can add information viewmodel? or shove in right direction great.
var peoplemodel = db.people.include("employeecontacts").single(g => g.peopleid == peopleid);
what type of peoplemodel
in case?
seeing returning directly database, looks has not yet been 'converted' or 'projected' actual viewmodel view expects.
you need this:
addorganizationviewmodel vm = new addorganizationviewmodel(); vm.organizations = new organizations() { someorganizationproperty = peoplemodel.someproperty, }; vm.people = new people() { somepeopleproperty = peoplemodel.someotherproperty, }; //etc other properties , types in viewmodel
when data has been set, can return addorganizationviewmodel
.
Comments
Post a Comment