Qiang
Qiang

Reputation: 3

ASP.NET : Unable to present data from view model in view

I'd just started using ASP.NET and are training through a lot of tutorials from youtube and googles that I can find.

But I can't seem to find a solutions to this.

My purpose is to generate a table view of Customers into the table to list as c1 and c2 using viewmodel.

My viewmodel code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Alpha.Models;

namespace Alpha.ViewModels
{
    public class ListCustomersViewModel
    {
        public List<Customer> Customers { get; set; }
    }
}

My Controller code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Alpha.Models;
using Alpha.ViewModels;


namespace Alpha.Controllers
{
    public class CustomersController : Controller
    {
        // GET: Customers/ListCustomer
        public ActionResult ListCustomers()
        {
            var customers = new List<Customer>
            {
                new Customer { Name = "C1" , Id = 1},
                new Customer { Name = "C2" , Id = 2}
            };
            var viewModel = new ListCustomersViewModel
            {
                Customers = customers
            };
            return View(viewModel);
        }
    }
}

My View code:

@model Alpha.ViewModels.ListCustomersViewModel
@{
    ViewBag.Title = "ListCustomers";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Customers</h1>

<div class="container">
    <table class="table">
        <thread>
            <tr>
                <th>Customer</th>
            </tr>
        </thread>
        <tbody>
            <tr>
                <td>*I DONT KNOW WHAT I SHOULD TYPE HERE TO PASS THE INFORMATION IN (C1)</td>
                <td>*I DONT KNOW WHAT I SHOULD TYPE HERE TO PASS THE INFORMATION IN (C2)</td>
            </tr>
        </tbody>
    </table>
</div>

I really hope some one can give me a guide on this thing. Thank you.

Upvotes: 0

Views: 98

Answers (3)

Masoud Andalibi
Masoud Andalibi

Reputation: 3228

Your ViewModel Contains a List which means it contains more than one entity within, so basically its not as easy as passing a class to your model. for extracting the data from a list you need to use a loop, here as others have explained you can use foreach to show the data :

<tbody>
@foreach (var item in Model.Customers)
{
    <tr>
        <td>
            //one way to show the data
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            //or you can simple call the item object and declare the property you want to show
            @item.Id
        </td>
    </tr>
}
</tbody>

Upvotes: 0

Filip Kov&#225;č
Filip Kov&#225;č

Reputation: 579

At first use in view @model IEnumerable<Class that you are using> then you can use in table

  <tbody>
       @foreach (Customer cust in Model) 
       {
       <tr>
           <td>@Html.DisplayFor(modelItem => cust.Name)</td>
       </tr>
       }
  </tbody>

Here you can find how working with .net.

Upvotes: 0

Steve
Steve

Reputation: 216293

You need to loop over your model and extract the info to build the table

<tbody>
@foreach (var item in Model.Customers)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
    </tr>
}
</tbody>

Upvotes: 2

Related Questions