2013-07-17 1 views
1

내가 원하는 것은 두 개의 다른 테이블의 정보로 많은 레이블을 채우는 것입니다. 제출 버튼을 누르면, 내 Post 메소드를 치고 사용자가 입력 한 정보를 저장하고 정보가 자동으로 DB에 채워지지만 Get 메소드에 문제가 있습니다. 내 컨트롤러 내 가져 오기 방법은 다음과 같습니다ViewModel을 사용하여 여러 테이블의 값이있는 페이지로드

[HttpGet] 
    public ActionResult AddOrganization(int peopleID = 1) 
    { 
     var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID); 
     return View("../Setup/AddOrganization", peopleModel); 
    } 

을하지만, 나는이 모양이 페이지에 대한 가져 오기 및 포스트 방법을 위해 필요한 모든 테이블을 포함하는 뷰 모델이있어 :

:
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; } 
    } 
뷰가 처음로드 그래서

, 위의 가져 오기 방법이라고, 나는 여기에

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.People_8080C33752A42A0C994331F5015BCCFCEB99B3ECD7AB8CA2BB11ABE67851B81B', but this dictionary requires a model item of type 'MusicStore.ViewModels.AddOrganizationViewModel'. 

말하는 오류도 내보기입니다 얻을

<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> 

어떻게 그것이 단지 뷰 모델 유형을 취할 수있는 가져 오기 방법을 통해 뷰에 필요한 정보를 보내려면 어떻게합니까? 이 정보를 내 ViewModel에 추가 할 수있는 방법이 있습니까? 올바른 방향으로 어떤 도움이나 밀림은 좋을 것입니다.

답변

1
var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID); 

이 경우 peopleModel의 유형은 무엇입니까?

데이터베이스에서 직접 반환하는 것처럼 보이기 때문에 뷰가 기대하는 실제 뷰 모델로 '변환'또는 '투영'되지 않은 것으로 보입니다.

당신은 아마 같은 것을 할 필요가있을 것이다 : 모든 데이터가 설정되어있는 경우

AddOrganizationViewModel vm = new AddOrganizationViewModel(); 
vm.Organizations = new Organizations() 
{ 
someOrganizationProperty = peopleModel.SomeProperty, 
}; 
vm.People = new People() 
{ 
somePeopleProperty = peopleModel.SomeOtherProperty, 
}; 
//etc for the other properties and types in your viewmodel 

, 당신은 AddOrganizationViewModel을 반환 할 수 있습니다.

+1

내가 원했던 것과 똑같이 작동했다! 도움 주셔서 대단히 감사합니다! – SantasNotReal

0
var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID); 

이 경우 AddOrganizationViewModel 개체가 생성되지 않습니다. 끝 부분에 Select 절을 추가하거나 해당 객체의 데이터를 사용하여 새 뷰 모델을 채 웁니다.

관련 문제