[英]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. 我有一个Index操作方法,如下所示。我正在将提供者列表传递给视图。
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 我在控制器中看到了提供商列表
But I dont see the same list in the view as shown 但是我在视图中看不到相同的列表
Am I doing something wrong.Please guide me in the right directions.Thanks. 我做错什么了,请指导我正确的方向。
To check the count of the providers list inside the view use the following property 要检查视图中提供者列表的数量,请使用以下属性
@Model
In your view you can iterate like this on the items, and for example am displaying FirstName
of each Provider
in the List: 在您看来,您可以在项目上进行这样的迭代,例如在列表中显示每个Provider
FirstName
:
@foreach(var item in Model)
{
<h1>@item.FirstName </h1>
}
Using your html: 使用您的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>
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>
you can't access the razor Model object via knockout 您无法通过敲除访问剃刀模型对象
you need to receive your data as json result 您需要将数据作为json结果接收
please refer to this article http://www.codeproject.com/Articles/424642/Customer-KnockoutJS-and-MVC-demo-using-JSON 请参考这篇文章http://www.codeproject.com/Articles/424642/Customer-KnockoutJS-and-MVC-demo-using-JSON
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.