MVC将ViewModel返回到列表

MVC将ViewModel返回到列表

本文介绍了C#MVC将ViewModel返回到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定发布一个新问题,而不是将其添加到现有问题中,因为这不是同一问题.我按照注释中的另一个问题创建了ViewModel.

I decided to post a new question rather then add to the existing because it is not the same issue. I followed the comment to the other question to create a ViewModel.

到目前为止,我还没有必须从ViewModel中列出.我确实在网上搜索了一下,看看是否可以找到一个例子,但是我发现的只是;如何在ViewModel中创建列表,我已经知道该怎么做.

I have not had, up to this point, had to list from a ViewModel. I did search online to see if I could find an example but all I found was; How to create a list in a ViewModel, which I already know how to do.

我下面有一个ViewModel.我不确定它的结构是否正确,但控制器返回的 count = 0

I have a ViewModel below. I am not sure that it is structured properly or not but the controller is returning count = 0

控制器:

    public ActionResult Index()
    {
        List<AccountingViewModels> list = new List<AccountingViewModels>();
        return View(list);
    }

视图具有以下内容: @model IEnumerable< BestenEquipment.Models.AccountingViewModels>

我确实尝试执行此 @model List< BestenEquipment.Models.AccountingViewModels>

但是出现错误'List< AccountingViewModels>'不包含说明"的定义,也没有扩展方法说明"接受类型为"List< AccountingViewModels>"的第一个参数可以找到(您是否缺少using指令或程序集引用?)

控制器仍为 count = 0

IEnumerable不会出错,只是给我一个空表.因此,我认为IEnumerable是必经之路.我只需要修复控制器即可.

The IEnumerable does not error out it just gives me an empty table. So I am thinking the IEnumerable is the way to go. I just need to fix the controller.

如果有人可以指出正确的方向或告诉我控制器中我有什么错,那将大有帮助.

If someone could point me in the right direction or tell me what I have wrong in my Controller it would help a great deal.

这是ViewModel:

Here is the ViewModel:

public class AccountingViewModels
{
    [Key]
    public Guid TransactionId { get; set; }
    public string Description { get; set; }
    [Display(Name = "Company")]
    public int? CompanyId { get; set; }
    [Display(Name = "Vendor")]
    public Guid? VendorId { get; set; }
    [Display(Name = "Rec Chk #")]
    public string InCheckNumber { get; set; }
    [Display(Name = "Sent Chk #")]
    public string OutCheckNumber { get; set; }
    [Display(Name = "Invoice #")]
    public string InvoiceNumber { get; set; }
    [Display(Name = "PO #")]
    public string PurchaseOrderNumber { get; set; }
    [Display(Name = "Ledger Acct")]
    public Guid LedgerAccountId { get; set; }
    [Display(Name = "Credit")]
    public decimal? DebitAmount { get; set; }
    [Display(Name = "Debit")]
    public decimal? CreditAmount { get; set; }
    [Display(Name = "Transaction")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = false)]
    public DateTime TransactionDate { get; set; }
    [Display(Name = "Modified By")]
    public string ModifiedBy { get; set; }
    [Display(Name = "Modified")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = false)]
    public DateTime? ModifiedDate { get; set; }
    public string SavedDocument { get; set; }
    [Display(Name = "Created")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = false)]
    public DateTime CreatedDate { get; set; }
    [Display(Name = "Created By")]
    public string CreatedBy { get; set; }
    public bool IsCredit { get; set; }
    public bool IsDebit { get; set; }
    public Guid Type { get; set; }

    [ForeignKey("LedgerAccountId")]
    public LedgerAccount LedgerAccount { get; set; }
    [ForeignKey("CompanyId")]
    public CompanyNames Company { get; set; }
    [ForeignKey("VendorId")]
    public Vendors Vendor { get; set; }
}

这很可能是一个简单的修复程序,我只需要轻按即可.

This is most likely a simple fix, I just need a nudge..

使用数据库模型是这样的: return View(db.Transaction.ToList());

Using the db model is like this: return View(db.Transaction.ToList());

我需要执行同样的操作,但是要使用ViewModel.

I need to do the same but with a ViewModel..

感谢您的帮助!

谢谢

更新:

这是我原始问题的链接,并被告知创建一个viewModel.原始问题

Here is the link to my Original problem and was told to create a viewModel..Original Issue

更新:

添加视图:

@model List<BestenEquipment.Models.AccountingViewModels>
@{
   Layout = "~/Views/Shared/_DashboardLayout.cshtml";
}


@section ScriptsOrCss
{
    @Html.Action("DataTableCssJs", "Layout")
}
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<section class="content-header">
<h1>
    Overview List
    <small> Transactions </small>
</h1>
</section>
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
    <div class="col-md-12">
        @if (ViewBag.Create == true)
        {
            <div class="box box-solid box-primary">
                <div class="box-body">
                    <!-- Split button -->
                    <div class="margin">
                        <div class="btn-group">
                            @Ajax.ModalDialogActionLink("Create Quick", "Create", "Create ", "btn btn-info btn-sm")
                        </div>
                        <div class="btn-group">
                            <a href="~/Tranaction/Create" class="btn btn-warning btn-sm">Create Full</a>
                        </div>
                    </div>
                    <!-- flat split buttons -->
                </div><!-- /.box-body -->
            </div>
        }

        <div class="box box-primary">
            <div class="box-header">
                <h3 class="box-title">Orders</h3>
            </div><!-- /.box-header -->
            <div class="box-body table-responsive">
                <table class="table table-striped table-hover table-bordered" id="TransactionListTable">
                    <thead>
                        <tr>
                            <th>
                                @Html.DisplayNameFor(model => model.Description)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.Company.CompanyName)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.Vendor.Name)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.InCheckNumber)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.OutCheckNumber)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.InvoiceNumber)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.PurchaseOrderNumber)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.LedgerAccountId)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.DebitAmount)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.CreditAmount)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.TransactionDate)
                            </th>
                            <th>
                                CRUD
                            </th>
                            <th>
                                Actions
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.Items)
                        {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Description)
                            </td>
                            <td>
                                @if (item.CompanyId != null)
                                {
                                    @Html.DisplayFor(modelItem => item.Company.CompanyName)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }

                            </td>
                            <td>
                                @if (item.VendorId != null)
                                {
                                    @Html.DisplayFor(modelItem => item.Vendor.Name)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }

                            </td>
                            <td>
                                @if (item.InCheckNumber != null)
                                {
                                    @Html.DisplayFor(modelItem => item.InCheckNumber)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }
                            </td>
                            <td>
                                @if (item.OutCheckNumber != null)
                                {
                                    @Html.DisplayFor(modelItem => item.OutCheckNumber)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }

                            </td>
                            <td>
                                @if (item.InvoiceNumber != null)
                                {
                                    @Html.DisplayFor(modelItem => item.InvoiceNumber)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }

                            </td>
                            <td>
                                @if (item.PurchaseOrderNumber != null)
                                {
                                    @Html.DisplayFor(modelItem => item.PurchaseOrderNumber)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.LedgerAccount.Title)
                            </td>
                            <td>
                                @if (item.IsDebit == true)
                                {
                                    @Html.DisplayFor(modelItem => item.DebitAmount)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }

                            </td>
                            <td>
                                @if (item.IsCredit == true)
                                {
                                    @Html.DisplayFor(modelItem => item.CreditAmount)
                                }
                                else
                                {
                                    <b>N/A</b>
                                }
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.TransactionDate)
                            </td>
                            <td>

                                @if (ViewBag.Edit == true)
                                {
                                    @Ajax.ModalDialogActionLink("Edit", "Edit", "Edit", "btn btn-warning btn-sm", new { id = item.TransactionId })
                                }

                                @if (ViewBag.Read == true)
                                {
                                    @Ajax.ModalDialogActionLink("Details", "Details", "Transaction Details", "btn btn-info btn-sm", new { id = item.TransactionId })
                                }

                                @if (ViewBag.Delete == true)
                                {
                                    @Ajax.ActionLink("Delete", "Delete", "Tranaction", new { id = item.TransactionId },
                                             new AjaxOptions()
                                             {
                                                 HttpMethod = "Delete",
                                                 Confirm = "Are you sure you want to delete " + item.Description + "Transaction",
                                             }, new { @class = "btn btn-danger btn-sm Delete" })
                                }
                            </td>
                            <td>

                            </td>
                        </tr>

                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

推荐答案

我复制了您的错误.试试这个.

I replicated your error. Try this.

代替这个:

@model List<BestenEquipment.Models.AccountingViewModels>

使用此:

@model IEnumerable<BestenEquipment.Models.AccountingViewModels>

最后,在您的 foreach 循环中删除 Items 一词,它应该是:

Then lastly, remove the Items word on your foreach loop, it should be:

@foreach (var item in Model)

这篇关于C#MVC将ViewModel返回到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:59