1

I am trying to make an Intranet that allows Customers to be added to a database and then allow further information to be added about them. The main issue is that for things like employee info, there is multiple employees and so I tried to put the customer information models inside the AddCustomers model details page, however this message was returned.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Only one 'model' statement is allowed in a file.

Source Error:

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

Source File: /Views/AddCustomers/Details.cshtml Line: 38

Is there a way to get around only one model statement being allowed in a file or is there a better way to achieve what I am trying to do?

Edit:

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

I want to be able to add customer info e.g. EmployeeInfo and ContactInfo after creating a customer in AddCustomers. However with there being multiple employees I need to have separate tables for the information and want these tables/models inside the AddCustomers details view.

Riley
  • 17
  • 2
  • 10
  • Create a view model containing the properties form all the models you want and pass that to the view - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Nov 21 '17 at 10:44
  • @StephenMuecke This looks promising, but can I just check. Will this allow each individual customers ViewModel (e.g. EmployeeInfo) to be shown separately or will this work like a partial view and show every customers EmployeeInfo? – Riley Nov 21 '17 at 10:50
  • Not clear what you asking because I do not know what your view is for. Assuming for example your have a view that displays a form for adding a new customer but also displays existing customers, your view model might contain the properties of a `Customer` - say `string FirstName` etc and another property `IEunmerable` for displaying the collection in the view. –  Nov 21 '17 at 10:55
  • @StephenMuecke So what you are saying is that for each `Customer` the `EmployeeInfo` will be separate? And that it won't all collate into one `EmployeeInfo` table that will then show on each Customers details page? – Riley Nov 21 '17 at 11:05
  • Sorry, but I don't understand what your asking because I do not know what your models are and what it is you want to display in the view. –  Nov 21 '17 at 11:07
  • @StephenMuecke I have added the requested information. To give a bit of context, what I am aiming for is similar to the Contoso University Student Grades table inside Student Details. But instead with multiple grade tables inside the Student Details page that still have CRUD capabilities and not just read only. – Riley Nov 21 '17 at 11:35

1 Answers1

0

As Stephen said, you need a ViewModel which should be everything you need for your View (AddCustomer). So from you description you might use something like:

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

You would populate this ViewModel in your controller's AddCustomer [GET]. Then your view would refer to the view model:

@model AddCustomerViewModel

For the employee info, since it is a collection you would use a foreach loop:

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

For the CRUD part, you will need to add a form and controller POST as shown in the demo.

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • After implementing this code, when I tried to scaffold a controller I received this message back. `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.` – Riley Nov 22 '17 at 13:18
  • If you want to scaffold, use your entity model and then change to the ViewModel and add the extra info. Add your validation and other annotations to the ViewModel. I'm not sure what AddCustomers is above. That sounds like a ViewModel but looks like an entity model. You should probably have a Customer class with navigation properties for Employees and Contacts, but that is another story. – Steve Greene Nov 22 '17 at 15:01