Reputation: 482
I'm using MVC3 and webgrid on a system. I like to know if it's possible to implement a search engine.
My controller has
Return View(service.list)
and return around 50000 records, so it will be nice to search something inside results.
Regards.
Upvotes: 2
Views: 5500
Reputation: 482
I found the solution....
The view (using Razor):
@Using Html.BeginForm("ListarAsegurados", "PolizasLayout", New AjaxOptions With {.UpdateTargetId = "divGrid"})
Dim varCondicionesDeBusqueda As IDictionary(Of String, String) = New Dictionary(Of String, String)
If Not IsNothing(TempData("CondicionesDeBusqueda")) Then
varCondicionesDeBusqueda = TryCast(TempData("CondicionesDeBusqueda"), Dictionary(Of String, String))
End If
TempData("CondicionesDeBusqueda") = varCondicionesDeBusqueda
Dim varDocumentoAsegurado As String = If(varCondicionesDeBusqueda.Keys.Contains("DOCUMENTO_ASEGURADO"), varCondicionesDeBusqueda("DOCUMENTO_ASEGURADO"), String.Empty)
Dim varNombresAsegurado As String = If(varCondicionesDeBusqueda.Keys.Contains("Asegurado"), varCondicionesDeBusqueda("Asegurado"), String.Empty)
@<fieldset>
<legend>Filtros de Búsqueda</legend>
<table>
<tr>
<td>
@Html.RadioButton("Grupo", "", True, New With {.class = "letraingreso", .style = "width:30px", .id = "RadioButton1", .onclick = "CambiarEstadoControles(this)"})
</td>
<td style="border-width:0px; width:120px">
@Html.Label("ID_TIPO_DOCUMENTO", "Tipo Documento:")
</td>
<td style="border-width:0px">
@Html.DropDownList("ID_TIPO_DOCUMENTO", Nothing, New With {.class = "letraingreso", .style = "width:250px"})
</td>
<td style="width:50px"></td>
<td style="border-width:0px">
@Html.Label("DOCUMENTO_ASEGURADO", "No. Documento:")
@Html.TextBox("DOCUMENTO_ASEGURADO", varDocumentoAsegurado, New With {.class = "letraingreso", .style = "width:200px", .onkeyup = "if(this.value.match(/\D/))this.value=this.value.replace(/\D/g,'')"})
</td>
</tr>
<tr>
<td>
@Html.RadioButton("Grupo", "", False, New With {.class = "letraingreso", .style = "width:30px", .id = "RadioButton2", .onclick = "CambiarEstadoControles(this)"})
</td>
<td style="border-width:0px; width:120px">
@Html.Label("Asegurado", "Apellidos / Nombres:")
</td>
<td style="border-width:0px">
@Html.TextBox("Asegurado", varNombresAsegurado, New With {.class = "letraingreso", .style = "width:400px; text-transform:uppercase", .disabled = "disabled"})
</td>
</tr>
</table>
<br />
<table>
<tr>
<td style="border-width:0px; width:145px">
<input type ="submit" value="Buscar" style="width:90px" />
</td>
</tr>
</table>
</fieldset>
@<br />
@<p>
@Html.ActionLink(" ", "Create", "Asegurado", New With {.area = "Polizas"}, New With {.class = "imgNuevo"})
</p>
@<div id="divGrid">
@grid.GetHtml(
tableStyle:="webgrid",
headerStyle:="webgrid-header",
footerStyle:="webgrid-footer",
alternatingRowStyle:="webgrid-alternating-row",
selectedRowStyle:="webgrid-selected-row",
rowStyle:="webgrid-row-style",
mode:=WebGridPagerModes.All,
firstText:="<< Inicio",
previousText:="< Anterior",
nextText:="Siguiente >",
lastText:="Fin >>",
columns:=grid.Columns(
grid.Column(format:=Function(Model) Html.ActionLink(" ", "Edit", "Asegurado", New With {.area = "Polizas", .parID = Model.ID_ASEGURADO}, New With {.class = "imgEditar", .title = "Modificar"}), style:="webgrid-width-images"),
grid.Column(format:=Function(Model) Html.ActionLink(" ", "Delete", "Asegurado", New With {.area = "Polizas", .parID = Model.ID_ASEGURADO}, New With {.class = "imgEliminar", .title = "Eliminar"}), style:="webgrid-width-images"),
grid.Column("DOCUMENTO_ASEGURADO", "No. Documento"),
grid.Column("PRIMER_APELLIDO_ASEGURADO", "Asegurado", format:=Function(Model) Model.PRIMER_APELLIDO_ASEGURADO & " " & Model.SEGUNDO_APELLIDO_ASEGURADO & " " & Model.NOMBRES_ASEGURADO),
grid.Column("DIRECCION_DOMICILIO_ASEGURADO", "Dirección"),
grid.Column("EMAIL_ASEGURADO", "eMail")
)
)
</div>
End Using
Controller:
Function ListarAsegurados() As ActionResult
Dim varListado As SelectList
Dim varCondicionesDeBusqueda As IDictionary(Of String, String) = New Dictionary(Of String, String)
If Request.Form.AllKeys.Length > 0 Then
varCondicionesDeBusqueda.Add("DOCUMENTO_ASEGURADO", Request("DOCUMENTO_ASEGURADO"))
varCondicionesDeBusqueda.Add("Asegurado", Request("Asegurado"))
Else
Dim values As Object = Nothing
If TempData.TryGetValue("CondicionesDeBusqueda", values) Then
varCondicionesDeBusqueda = TryCast(values, Dictionary(Of String, String))
End If
End If
TempData("CondicionesDeBusqueda") = varCondicionesDeBusqueda
Dim varDocumentoAsegurado As String = GetSearchConditionValue(varCondicionesDeBusqueda, "DOCUMENTO_ASEGURADO")
Dim varNombresAsegurado As String = GetSearchConditionValue(varCondicionesDeBusqueda, "Asegurado")
If String.IsNullOrEmpty(varDocumentoAsegurado) And String.IsNullOrEmpty(varNombresAsegurado) Then
varListado = New SelectList(varServicio.ListarTiposDocumentos, "ID_TIPO_DOCUMENTO", "DESC_TIPO_DOCUMENTO")
ViewData("ID_TIPO_DOCUMENTO") = varListado
Return View(varServicio.ListarAsegurados)
Else
varListado = New SelectList(varServicio.ListarTiposDocumentos, "ID_TIPO_DOCUMENTO", "DESC_TIPO_DOCUMENTO", CLng(Request.Form("ID_TIPO_DOCUMENTO")))
ViewData("ID_TIPO_DOCUMENTO") = varListado
If String.IsNullOrEmpty(varDocumentoAsegurado) Then
Return View(varServicio.ListarAseguradosPorApellidosNombres(varNombresAsegurado))
Else
Return View(varServicio.ListarAseguradosPorDocumento(CLng(varListado.Items(0).ID_TIPO_DOCUMENTO), varDocumentoAsegurado))
End If
End If
End Function
The function:
Public Function GetSearchConditionValue(ByVal parCondicionesDeBusqueda As IDictionary(Of String, String), ByVal parClave As String) As String
Dim varValorTemporal As String = String.Empty
'If parCondicionesDeBusqueda IsNot Nothing AndAlso parCondicionesDeBusqueda.Keys.Contains("StudentName") Then
If parCondicionesDeBusqueda IsNot Nothing Then
parCondicionesDeBusqueda.TryGetValue(parClave, varValorTemporal)
End If
Return varValorTemporal
End Function
You can found the original post at http://www.cnblogs.com/jack86514/archive/2010/10/26/1861109.html
Upvotes: 1
Reputation: 6851
You can do the search by filtering your result.
You can add a textbox with the field to search and before returning all your result filter with the content of your textbox
Upvotes: 0