Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

asp.net mvc - C# MVC Return ViewModel to List

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.

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.

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

Controller:

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

The View Has This: @model IEnumerable<BestenEquipment.Models.AccountingViewModels>

I did try to do this @model List<BestenEquipment.Models.AccountingViewModels>

But got an Error 'List<AccountingViewModels>' does not contain a definition for 'Description' and no extension method 'Description' accepting a first argument of type 'List<AccountingViewModels>' could be found (are you missing a using directive or an assembly reference?)

Controller is still count = 0

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.

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..

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

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

Thanks for your help!

Thanks

UPDATE:

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

UPDATE:

Adding View:

@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.L

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Public class AcctModel
{
public IEnumerable<AccountingViewModels> Items {get; set;}
}

In view.cshtml

foreach(var item in Model.Items)
{
// Your code here
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...