【问题标题】:Is there a way to have multiple models in another models details page?有没有办法在另一个模型详细信息页面中有多个模型?
【发布时间】:2017-11-21 10:43:21
【问题描述】:

我正在尝试创建一个允许将客户添加到数据库中的 Intranet,然后允许添加有关他们的更多信息。主要问题是,对于员工信息之类的内容,有多个员工,因此我尝试将客户信息模型放在 AddCustomers 模型详细信息页面中,但是返回了此消息。

解析器错误

描述:解析资源时出错 需要服务此请求。请查看以下具体 解析错误详细信息并适当地修改您的源文件。

解析器错误消息:文件中只允许有一个“模型”语句。

来源错误:

Line 36: 
Line 37: <!-------------------------------------------------------------------------->
Line 38: @model IEnumerable<Intranet.Models.EmployeeInfo>
Line 39: 
Line 40: @{

源文件:/Views/AddCustomers/Details.cshtml 行:38

有没有办法绕过文件中只允许一个模型语句,或者有更好的方法来实现我想要做的事情?

编辑:

namespace Intranet.Models
{
    public class AddCustomers
    {
        public int AddCustomersID { get; set; }
        public string CompanyName { get; set; }
        public string Status { get; set; }
    }

    public class EmployeeInfo
    {
        public int EmployeeInfoID { get; set; }
        public string Forename { get; set; }
        public string Surname { get; set; }
        public string Title { get; set; }
        public string Mobile { get; set; }
        public string Telephone { get; set; }
        public string Email { get; set; }
    }

    public class ContactInfo
    {
        public int ContactInfoID { get; set; }
        public string Code { get; set; }
        public string Address { get; set; }
        public string Postcode { get; set; }
        public string Telephone { get; set; }
    }

我希望能够添加客户信息,例如EmployeeInfoContactInfoAddCustomers 中创建客户后。但是,由于有多名员工,我需要为信息提供单独的表格,并希望这些表格/模型位于 AddCustomers 详细信息视图中。

【问题讨论】:

  • 创建一个视图模型,其中包含您想要的所有模型的属性并将其传递给视图 - What is ViewModel in MVC?
  • @StephenMuecke 这看起来很有希望,但我可以检查一下。这是否允许单独显示每个客户的 ViewModel(例如 EmployeeInfo),或者这会像部分视图一样工作并显示每个客户的 EmployeeInfo?
  • 不清楚你在问什么,因为我不知道你的观点是什么。例如,假设您有一个显示用于添加新客户的表单但也显示现有客户的视图,您的视图模型可能包含Customer 的属性 - 比如string FirstName 等和另一个属性IEunmerable&lt;EmployeeInfo&gt; 用于显示集合在视图中。
  • @StephenMuecke 所以你的意思是对于每个Customer EmployeeInfo 将是分开的?而且它不会全部整理到一个EmployeeInfo 表中,然后将显示在每个客户详细信息页面上?
  • 抱歉,我不明白您的要求,因为我不知道您的模型是什么以及您想在视图中显示什么。

标签: c# asp.net-mvc class model entity-framework-6


【解决方案1】:

正如斯蒂芬所说,您需要一个 ViewModel,它应该是您的 View (AddCustomer) 所需的一切。因此,根据您的描述,您可能会使用以下内容:

public class AddCustomerViewModel
{
    public AddCustomerViewModel()
    {
         Employees = new List<EmployeeInfo>();
         ContactInfo = new ContactInfo;
    }

    public int AddCustomersID { get; set; }
    public string CompanyName { get; set; }
    public string Status { get; set; }

    // Contact info - If there is only 1, you could just add the properties to the ViewModel
    public ContactInfo ContactInfo { get; set; }

    // Related employee:
    public List<EmployeeInfo> Employees { get; set; }
}

您将在控制器的 AddCustomer [GET] 中填充此 ViewModel。然后您的视图将引用视图模型:

@model AddCustomerViewModel

对于员工信息,由于它是一个集合,您可以使用 foreach 循环:

// build table and header
@foreach (var employee in Model.Employees)
{
     // kick out a <tr><td>...</td></tr> for each employee
}

对于 CRUD 部分,您需要添加一个表单和控制器 POST,如演示中所示。

【讨论】:

  • 实现此代码后,当我尝试搭建一个控制器时,我收到了此消息。 Intranet.Models.AddCustomersViewModel: : EntityType 'AddCustomersViewModel' has no key defined. Define the key for this EntityType. AddCustomersViewModel: EntityType: EntitySet 'AddCustomersViewModel' is based on type 'AddCustomersViewModel' that has no keys defined.
  • 如果您想搭建脚手架,请使用您的实体模型,然后更改为 ViewModel 并添加额外信息。将您的验证和其他注释添加到 ViewModel。我不确定上面的 AddCustomers 是什么。这听起来像一个 ViewModel,但看起来像一个实体模型。您可能应该有一个带有员工和联系人导航属性的 Customer 类,但那是另一回事了。
猜你喜欢
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 2016-03-25
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
相关资源
最近更新 更多