ASP.NET Core - 2 @model binding

asp.net-core asp.net-core-mvc c# entity-framework-core

Question

I have problem with binding 2 models in 1 view. Here is my view code:

@model IEnumerable<PEMCOLoan.DAL.Entities.Models.Employee>
@model PEMCOLoan.DAL.Entities.Models.Employee

The IEnumerable uses to generate all employees into tables and the other one will be used to add/edit/delete... I already have functionalities on those operator how ever when viewing data I got error.

Is there anyway that I could bind 2 the same model with different function at the same time in 1 view?

1
1
4/2/2017 2:52:17 AM

Accepted Answer

Is there anyway that I could bind 2 the same model with different function at the same time in 1 view?

No.

Create one view-model that has both as properties.

public class MyViewModel {
    public IEnumerable<PEMCOLoan.DAL.Entities.Models.Employee> Employees { get; set;}
    public PEMCOLoan.DAL.Entities.Models.Employee Employee { get; set;}
}

that way when populated and passed to the view you can access them both via the wrapping model.

@model MyViewModel

@foreach(var e in Model.Employees) {
  <p>@e.Property</p>
}

<p>@Model.Employee.Property</p>
0
4/2/2017 2:48:04 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow