我想要做的是让许多标签填充来自 2 个不同表的信息。当按下提交按钮时,它会点击我的 Post 方法并保存用户输入的信息并将信息自动填充到数据库中,但是我的 Get 方法有问题。我的控制器中的 Get 方法如下所示:
[HttpGet]
public ActionResult AddOrganization(int peopleID = 1)
{
var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
return View("../Setup/AddOrganization", peopleModel);
}
但是,我有一个看起来像这样的 viewModel,其中包含此页面的 Get 和 Post 方法所需的所有表:
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; }
}
因此,当第一次加载视图并调用上面的 Get 方法时,我收到一条错误消息
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 类型时,如何通过 Get 方法将所需的信息发送到视图?有没有办法可以将此信息添加到我的 ViewModel 中?任何朝着正确方向的帮助或推动都会很棒。