A NewBie
A NewBie

Reputation: 181

Passing a list from Controller to View in ASP.NET MVC 4

I have an Index action method as follows.I am passing a list of Providers to the View.

        public ActionResult Index()
    {
        Provider providerList = new Provider();
        List<Provider> providers = DAL.GetListofProviders.ToList();
        return View(providers);
    }

In the View,I have the following code to receive the List of Providers.

     @model IEnumerable<DEMO_JAN14.Models.Provider>

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

 <head>
<title>LIST OF PROVIDERS</title>
</head>
 <body>
   <table class="table table-striped table-bordered table-hover">
      <tr>     
        <th>Provider Type</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Certification</th>
        <th>Specialization</th>
        <th>SSN</th>
        <th>Facility Name</th>
        <th>Contact No</th>
        <th>Contact Email</th>
        <th></th>  
      </tr>

<tbody data-bind="foreach: viewmodel">
  <tr>
        <td class="col-lg-2" data-bind="text: ProviderType"></td>
        <td class="col-lg-1" data-bind="text: FirstName"></td>
        <td class="col-lg-1" data-bind="text: LastName"></td>
        <td class="col-lg-1" data-bind="text: Certification"></>
        <td class="col-lg-1" data-bind="text: Specialization"></td>
        <td class="col-lg-1" data-bind="text: SSN"></td>
        <td class="col-lg-4" data-bind="text: FacilityName"></td>
        <td class="col-lg-4" data-bind="text: ContactNumber"></td>
        <td class="col-lg-1" data-bind="text: ContactEmail"></td>
        <td><a class="btn btn-danger" id="del" onclick = "return confirm('Are you sure, you want to delete');" data-bind="attr: { href: '/Provider/Delete/' + ProviderID }"> Delete </a>
        </td>
    </tr> 
</tbody>           

I see the list of Providers in the controller

enter image description here

But I dont see the same list in the view as shown

enter image description here

Am I doing something wrong.Please guide me in the right directions.Thanks.

Upvotes: 0

Views: 5596

Answers (4)

ahmed abdelnabi
ahmed abdelnabi

Reputation: 51

you can't access the razor Model object via knockout

you need to receive your data as json result

please refer to this article http://www.codeproject.com/Articles/424642/Customer-KnockoutJS-and-MVC-demo-using-JSON

Upvotes: 0

samvdst
samvdst

Reputation: 638

You can iterate like this (using your code)

@model IEnumerable<DEMO_JAN14.Models.Provider>

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

<head>
<title>LIST OF PROVIDERS</title>
</head>
 <body>
   <table class="table table-striped table-bordered table-hover">
      <tr>     
        <th>@Html.DisplayNameFor(m => m.ProviderType)</th>
        <th>@Html.DisplayNameFor(m => m.FirstName)</th>
        ...
      </tr>

@foreach (var item in Model)
  <tr>
        <td class="col-lg-2">@Html.DisplayFor(modelItem => item.ProviderType)</td>
        <td class="col-lg-1">@Html.DisplayFor(modelItem => item.FirstName)</td>
        ...
  </tr> 
</table>   
</body>

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

In your view you can iterate like this on the items, and for example am displaying FirstName of each Provider in the List:

@foreach(var item in Model)
{
 <h1>@item.FirstName </h1>
}

Using your html:

<tbody data-bind="foreach: viewmodel">
@foreach(var item in Model)
 {
  <tr>
        <td class="col-lg-2">@item.ProviderType</td>
        <td class="col-lg-1">@item.FirstName</td>
        ........................................
        ........................................
        // and so on other properties
        <td><a class="btn btn-danger" id="del" onclick = "return confirm('Are you sure, you want to delete');" href="@Url.Action("Delete","Provider")+ item.ProviderID }"> Delete </a>
 </td>
 </tr> 

Upvotes: 0

Hossam Barakat
Hossam Barakat

Reputation: 1397

To check the count of the providers list inside the view use the following property

@Model

Upvotes: 0

Related Questions