Home > Software design >  Display a list and add new item in on list asp.net core
Display a list and add new item in on list asp.net core

Time:12-09

I want to create a view that will display a list in a table above and below a form to input new items to the same list, but I get this error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[NAOC_ROW.Models.tbContPaymentHist]', but this ViewDataDictionary instance requires a model item of type 'NAOC_ROW.Models.tbContPaymentHist'.

Here is the view

    @model IEnumerable<NAOC_ROW.Models.tbContPaymentHist>

@{
    ViewData["Title"] = "Payment List";
   
<section >
    <div >

        <h3> Payment History</h3>
        <hr />

        <div >
            <div >
                <div >
                    <div >
                        <h3 >
                            <i ></i>
                            Last Completed Payments
                        </h3>
                    </div>
                    <div >

                        @if (Model.Count() > 0)
                        {

                            @*scrolling bootstarp table, check css in top of this page*@
                            <div >

                                <table >
                                    <thead>
                                        <tr>
                                            <th scope="col">
                                                @Html.DisplayNameFor(model => model.Datepaid)
                                            </th>
                                            <th scope="col">
                                                @Html.DisplayNameFor(model => model.Amtpaid)
                                            </th>
                                            <th scope="col">
                                                @Html.DisplayNameFor(model => model.Balance)
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>

                                       
                            </div>
                        }
                        else
                        {
                            <h5>No payments completed yet</h5>
                        }

                    </div>
                </div>
            </div>
        </div>

        <div >
            <div >
                <div >
                    <div >
                        <h3 >
                            <i ></i>
                            Add New Payment
                        </h3>
                    </div>

                    <div >
                        
                        <p><strong>Monthly payments:</strong> &#x20A6;@String.Format("{0:#,##0.00}", @ViewBag.expected)</p>
                        <p><strong>Current balance:</strong> &#x20A6;@String.Format("{0:#,##0.00}", @ViewBag.bal)</p>
                        
                        @*<input type="hidden" asp-for="@Model.ContractId" />*@
                        <hr />


                           
                </div>
            </div>
        </div>
    </div>

    <div  id="modal-info">
        <div >
            <div >
                <div >
                    <h4 >Info Modal</h4>
                    <button type="button"  data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div >
                    <p>One fine body&hellip;</p>
                    <input type="hidden" value="@ViewBag.bal" name="AmtExpected" />
                    <partial name="_AddNewPayment" />
                </div>
                <div >
                    <button type="button"  data-dismiss="modal">Close</button>
                    <button type="button" >Save changes</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->

</section>

@section Scripts{
    @{
        <partial name="_ValidationScriptsPartial" />
    }
}

I created a partial view as you can see in the script hoping to loose the error, but still did not work. I took out some parts of the code to reduce your time having to read the code. I left the necessary code.

Any help is welcome.

CodePudding user response:

According to the error message, you pass the value of type List<Model> to Paritl view and use @model.xxx.Model in the partial view . so you can change some code like this:

The first way : you can use @model IEnumerable<NAOC_ROW.Models.tbContPaymentHist> in partial view;

The second way : you need to pass a model to the partial view <partial name="_AddNewPayment" model="new tbContPaymentHist()"/>

  • Related