none
ASP.NET Core - InvalidOperationException

    Question

  • Hi! I have a problem with my code.  What I am trying to achieve is that I want to place 2 @model in 1 view. So what I did is like this:

    Model:

    using PEMCOLoan.DAL.Entities.Models;
    namespace prjPEMCOLoan.Models
    {
    public class ModelEmployee
    {
        public IEnumerable<Employee> lstEmployee { get; set; }
        public Employee employees { get; set; }
    }
    }


    Controller: 

    [HttpGet]
        public async Task<IActionResult> Index()
        {
            var getAllEmployees = await _Context.Employee.ToListAsync();
            return View(getAllEmployees);
        }

    View:

    @model prjPEMCOLoan.Models.ModelEmployee
    
    @{
        ViewBag.Title = "List of Employees";
    }
    <form class="navbar-form navbar-right" style="margin-bottom: 10px;">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
    </form>
    
    
    
    <p>
       <h3>List of Employees</h3>
        <button type="button" class="btn btn-info btn-sm openAdd" data-toggle="modal" data-target="#myModal2"><span class="glyphicon glyphicon-floppy-save" style="vertical-align:middle;margin-top: -5px"></span> Add</button>
    </p>
    
    <table class="table table-hover table-striped">
        <tr>
            <th>ID Employee</th>
            <th>Full Name</th>
            <th>Contact Number</th>
            <th>Position</th>
            <th>Action</th>
        </tr>
    
        @foreach (var item in Model.lstEmployee)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.EmployeeId)</td>
                <td>@Html.DisplayFor(modelItem => item.Fname)  @Html.DisplayFor(modelItem => item.Lname)</td>
                <td>@Html.DisplayFor(modelItem => item.PhoneNumber)</td>
                <td>@Html.DisplayFor(modelItem => item.Position)</td>
                <td>
                    <a class="btn btn-primary btn-sm" asp-controller="Employee" asp-action="Details" asp-route-id="@item.EmployeeId"><span class="glyphicon glyphicon-dashboard" style="vertical-align:middle;margin-top: -5px"></span> Details</a>
                    <button type="button" class="btn btn-warning btn-sm openEdit" data-toggle="modal" data-target="#myModal" data-emp-id="@item.EmployeeId"><span class="glyphicon glyphicon-edit" style="vertical-align:middle;margin-top: -5px"></span> Edit</button>
                    <button type="button" class="btn btn-danger btn-sm openDiag" data-toggle="modal" data-target="#myModal" data-emp-id="@item.EmployeeId"><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
                </td>
            </tr>
    
        }
    </table>

    What I want to do with my app is I want to view the data inside the cshtml file however there is an error in my code:

    I don't know exactly what to do with it since I'm new with developing asp.net core nor have good experience with asp or c# either.

    Would you help me with it?

    Thanks!

    Sunday, April 2, 2017 8:36 AM

Answers

  • You are passing wrong instance model instance back to view from controller action, your action code should look like:

    [HttpGet]
    public async Task<IActionResult> Index()
    {
    
       ModelEmployee model = new ModelEmployee();
       model.lstEmployee = await _Context.Employee.ToListAsync();
             
       return View(model);
    }
    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ajqportal Sunday, April 2, 2017 9:22 AM
    Sunday, April 2, 2017 9:17 AM

All replies

  • You are passing wrong instance model instance back to view from controller action, your action code should look like:

    [HttpGet]
    public async Task<IActionResult> Index()
    {
    
       ModelEmployee model = new ModelEmployee();
       model.lstEmployee = await _Context.Employee.ToListAsync();
             
       return View(model);
    }
    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ajqportal Sunday, April 2, 2017 9:22 AM
    Sunday, April 2, 2017 9:17 AM
  • Thanks! I didn't know that was so simple! Thanks @Ehsan Sajjad
    Sunday, April 2, 2017 9:23 AM