大家下午好,我的视图中有一个索引,显示一个餐馆桌(西班牙语本地)。我在视图中添加了一个复选框,我需要复选框在选中时过滤索引,如果没有则显示完整的索引。我不知道是否有办法用mvc,
做到这一点这是我的观点
@model IEnumerable<AutoPlanMCV.Models.Local>
<div class="row">
<table class="col-md-12">
<tr>
<th>Nuevo</th>
<th>Instaldo</th>
<th>Capacitar</th>
</tr>
<tr>
<td>@Model.Count(x => x.Estado.State == "Nuevo")</td>
<td>@Model.Count(x=> x.Estado.State =="Instalado")</td>
<td>@Model.Count(x=>x.Estado.State == "Capacitar")</td>
</tr>
</table>
</div>
<div class="row">
<label for="verBajas">Ver Bajas</label>
<input type="checkbox" name="verBajas" value="true" id="verBajas"/>
</div>
<div>
<p>
@Html.ActionLink("Crear nuevo Local", "Create")
<br />
@Html.ActionLink("Export to Excel", "ExportToExcel")
</p>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary list-panel" id="list-panel">
<div class="panel-heading list-panel-heading">
<h1 class="panel-title list-panel-title">POS PDS UY</h1>
</div>
<div class="panel-body">
<table id="assets-data-table" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>V.Id</th>
<th>Id</th>
<th>Comercio</th>
<th>Direccion</th>
<th>Telefono</th>
<th>Ingreso</th>
<th>Provincia</th>
<th>Estado</th>
<th>Bonificado</th>
<th>Premium</th>
<th>Gestionar</th>
</tr>
</thead>
<tbody>
@foreach (var asset in Model)
{
<tr>
<td>@asset.ViejoId</td>
<td>@asset.NuevoId</td>
<td>@asset.NombreComercio</td>
<td>@asset.Direccion</td>
<td>@asset.Telefono</td>
<td>@Html.DisplayFor(modelItem => asset.FechaInstalacion)</td>
<td>@asset.Provincia</td>
<td>@asset.Estado.State</td>
@if (asset.Bonificado == true)
{
string i = "Bonificado";
<td>@i</td>
}
else
{
string i = "No";
<td>@i</td>
}
@if (asset.Premium == true)
{
string i = "Premium";
<td>@i</td>
}
else
{
string i = "No";
<td>@i</td>
}
<td>
@Html.ActionLink("Edit", "Edit", new { id = asset.Id ,estadoid = asset.Estado.Id,proveedorid = asset.Proveedor.Id}) |
@Html.ActionLink("Details", "Details", new { id = asset.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = asset.Id })|
@Html.ActionLink("Comment", "AgregarComentario", new { id = asset.Id })
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
@section Scripts
{
<script type="text/javascript">
$(document).ready(function () {
$('#assets-data-table').DataTable();
});
</script>
}
这是我控制器中的actionResult
public ActionResult IndexAsset(bool verBajas = false)
{
var locales = db.Locales.Include(l => l.Estado).Include(l => l.Proveedor).Where(l => l.Proveedor.Nombre == "PDSUY");
if(verBajas)
{
locales = db.Locales.Include(l => l.Estado).Include(l => l.Proveedor).Where(l => l.Proveedor.Nombre == "PDSUY").Where(l => l.Estado.State != "Bajas");
} else if (verBajas == false)
{
locales = db.Locales.Include(l => l.Estado).Include(l => l.Proveedor).Where(l => l.Proveedor.Nombre == "PDSUY");
}
return View(locales.ToList());
}
如果我不将“verBajas”初始化为false,我会得到一个null错误,即使我将视图中的值设置为true。
任何想法都表示赞赏。
答案 0 :(得分:0)
请按照以下步骤解决您的问题。当您运行该程序并单击该复选框时,您的网格将根据需要从2个项目更改为1个项目。
创建此表和数据:
--instead of farout, use your database name
USE [farout]
GO
/****** Object: Table [dbo].[Estado] Script Date: 4/28/2017 4:26:47 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Estado](
[Id] [int] IDENTITY(1,1) NOT NULL,
[State] [varchar](10) NULL,
CONSTRAINT [PK_Estado] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Locale] Script Date: 4/28/2017 4:26:48 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Locale](
[ViejoId] [int] IDENTITY(1,1) NOT NULL,
[NuevoId] [int] NULL,
[NombreComercio] [int] NULL,
[Direccion] [varchar](10) NULL,
[Telefono] [varchar](10) NULL,
[FechaInstalacion] [int] NULL,
[Provincia] [varchar](10) NULL,
[Estado] [int] NULL,
[Bonificado] [bit] NULL,
[Premium] [bit] NULL,
[Proveedor] [int] NULL,
CONSTRAINT [PK_Locale] PRIMARY KEY CLUSTERED
(
[ViejoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Proveedor] Script Date: 4/28/2017 4:26:48 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Proveedor](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Nombre] [varchar](10) NULL,
[Estado] [int] NULL,
CONSTRAINT [PK_Proveedor] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Estado] ON
GO
INSERT [dbo].[Estado] ([Id], [State]) VALUES (1, N'Bajas')
GO
INSERT [dbo].[Estado] ([Id], [State]) VALUES (2, N'Not Bajas')
GO
SET IDENTITY_INSERT [dbo].[Estado] OFF
GO
SET IDENTITY_INSERT [dbo].[Locale] ON
GO
INSERT [dbo].[Locale] ([ViejoId], [NuevoId], [NombreComercio], [Direccion], [Telefono], [FechaInstalacion], [Provincia], [Estado], [Bonificado], [Premium], [Proveedor]) VALUES (2, 1, 1, N'direction', N'4446666666', 1, N'provincia', 1, 1, 1, 1)
GO
INSERT [dbo].[Locale] ([ViejoId], [NuevoId], [NombreComercio], [Direccion], [Telefono], [FechaInstalacion], [Provincia], [Estado], [Bonificado], [Premium], [Proveedor]) VALUES (3, 1, 1, N'two', N'222222', 1, N'two', 2, 1, 1, 1)
GO
SET IDENTITY_INSERT [dbo].[Locale] OFF
GO
SET IDENTITY_INSERT [dbo].[Proveedor] ON
GO
INSERT [dbo].[Proveedor] ([Id], [Nombre], [Estado]) VALUES (1, N'PDSUY', 1)
GO
SET IDENTITY_INSERT [dbo].[Proveedor] OFF
GO
ALTER TABLE [dbo].[Locale] WITH CHECK ADD CONSTRAINT [FK_Locale_Estado] FOREIGN KEY([Estado])
REFERENCES [dbo].[Estado] ([Id])
GO
ALTER TABLE [dbo].[Locale] CHECK CONSTRAINT [FK_Locale_Estado]
GO
ALTER TABLE [dbo].[Locale] WITH CHECK ADD CONSTRAINT [FK_Locale_Proveedor] FOREIGN KEY([Proveedor])
REFERENCES [dbo].[Proveedor] ([Id])
GO
ALTER TABLE [dbo].[Locale] CHECK CONSTRAINT [FK_Locale_Proveedor]
GO
右键单击您的模型文件夹并添加ADO.NET ENTITY DATA MODEL。称之为西班牙语并单击“确定”。数据库中的EF Designer突出显示,然后单击下一步。单击新连接。输入服务器名称,我使用。\ sqlexpress。如果您不使用Windows身份验证,请输入您的凭据。在下拉列表中选择您的数据库名称。点击没关系。如果您使用Windows身份验证,请等到向导提示您并单击“是包含敏感信息...”。将“在设置中保存连接...”下的项目复制到剪贴板。点击下一步。在表dbo下,选择三个表:Estado,Locale和Proveedor。点击完成。出现提示时单击“确定”两次。关闭你的edmx图。
在您的控制器中添加以下代码:
public class Local
{
public Locale LocalAttr { get; set; }
public IList<DateTime> FechaInstalacion { get; set; }
public bool theCheckbox { get; set; }
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult IndexAsset(string chkName)
{
IList<Local> listOfViewModels = FactoredOut(bool.Parse(chkName));
return View(listOfViewModels);
}
public ActionResult IndexAsset(bool verBajas = false)
{
IList<Local> listOfViewModels = FactoredOut(verBajas);
return View(listOfViewModels);
}
IList<Local> FactoredOut(bool verBajas)
{
//instead of faroutEntities9, use the copy and paste item your copied in previous step
using (faroutEntities9 db = new faroutEntities9())
{
var locales = db.Locales.Include(l => l.Estado1).Include(l => l.Proveedor1).
Where(l => l.Proveedor1.Nombre == "PDSUY");
if (verBajas)
{
locales = db.Locales.Include(l => l.Estado1).Include(l => l.Proveedor1).
Where(l => l.Proveedor1.Nombre == "PDSUY").Where(l => l.Estado1.State != "Bajas");
}
else if (verBajas == false)
{
locales = db.Locales.Include(l => l.Estado1).Include(l => l.Proveedor1).
Where(l => l.Proveedor1.Nombre == "PDSUY");
}
IList<Local> listOfViewModels = new List<Local>();
var list = locales.ToList();
Local localView = new Local { LocalAttr = list[0] };
localView.FechaInstalacion = new List<DateTime>();
localView.FechaInstalacion.Add(DateTime.Now);
localView.FechaInstalacion.Add(DateTime.Parse("12/12/12"));
listOfViewModels.Add(localView);
if (list.Count > 1)
{
var list2 = locales.ToList();
Local localView2 = new Local { LocalAttr = list[1] };
localView2.FechaInstalacion = new List<DateTime>();
localView2.FechaInstalacion.Add(DateTime.Now);
localView2.FechaInstalacion.Add(DateTime.Parse("12/12/12"));
listOfViewModels.Add(localView2);
}
return listOfViewModels;
}
}
在您的视图中(使用没有部分页面的完整视图)添加以下代码:
@model IEnumerable<except.Controllers.Local>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>IndexAsset</title>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
@*<link href="~/Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>*@
<link href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#assets-data-table').DataTable();
});
</script>
</head>
<body>
<div class="row">
<table class="col-md-12">
<tr>
<th>Nuevo</th>
<th>Instaldo</th>
<th>Capacitar</th>
</tr>
<tr>
<td>@Model.Count(x => x.LocalAttr.Estado1.State == "Nuevo")</td>
<td>@Model.Count(x => x.LocalAttr.Estado1.State == "Instalado")</td>
<td>@Model.Count(x => x.LocalAttr.Estado1.State == "Capacitar")</td>
</tr>
</table>
</div>
<div class="row">
@using (Html.BeginForm())
{
<label for="verBajas">Ver Bajas</label>
@*<input type="checkbox" name="verBajas" value="true" id="verBajas" />*@
//http://stackoverflow.com/questions/18721288/using-checkbox-submit-form-in-asp-net-mvc
@Html.CheckBox("chkName", false, new { onclick = "this.form.submit();" })
}
</div>
<div>
<p>
@Html.ActionLink("Crear nuevo Local", "Create")
<br />
@Html.ActionLink("Export to Excel", "ExportToExcel")
</p>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary list-panel" id="list-panel">
<div class="panel-heading list-panel-heading">
<h1 class="panel-title list-panel-title">POS PDS UY</h1>
</div>
<div class="panel-body">
<table id="assets-data-table" class="display table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>V.Id</th>
<th>Id</th>
<th>Comercio</th>
<th>Direccion</th>
<th>Telefono</th>
<th>Ingreso</th>
<th>Provincia</th>
<th>Estado</th>
<th>Bonificado</th>
<th>Premium</th>
<th>Gestionar</th>
</tr>
</thead>
<tbody>
@foreach (var asset in Model)
{
<tr>
<td>@asset.LocalAttr.ViejoId</td>
<td>@asset.LocalAttr.NuevoId</td>
<td>@asset.LocalAttr.NombreComercio</td>
<td>@asset.LocalAttr.Direccion</td>
<td>@asset.LocalAttr.Telefono</td>
<td>@Html.DisplayFor(modelItem => asset.FechaInstalacion)</td>
<td>@asset.LocalAttr.Provincia</td>
<td>@asset.LocalAttr.Estado1.State</td>
@if (asset.LocalAttr.Bonificado == true)
{
string i = "Bonificado";
<td>@i</td>
}
else
{
string i = "No";
<td>@i</td>
}
@if (asset.LocalAttr.Premium == true)
{
string i = "Premium";
<td>@i</td>
}
else
{
string i = "No";
<td>@i</td>
}
<td>
@Html.ActionLink("Edit", "Edit",
new
{
id = asset.LocalAttr.ViejoId,
estadoid = asset.LocalAttr.Estado1.Id,
proveedorid = asset.LocalAttr.Proveedor1.Id
}) |
@Html.ActionLink("Details", "Details", new { id = asset.LocalAttr.ViejoId }) |
@Html.ActionLink("Delete", "Delete", new { id = asset.LocalAttr.ViejoId })|
@Html.ActionLink("Comment", "AgregarComentario",
new { id = asset.LocalAttr.ViejoId })
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>