I would like to know how I can add an edit and search with Dapper library, I already made the insert and delete method. I'm working with MVC C # VS 2013
CONTROLLER
namespace WebApplication1.Controllers
{
public class Default1Controller : Controller
{
[ActionName("Index")]
[HttpGet]
public ActionResult GetIndex()
{
NameValueCollection queryString = Request.QueryString;
IDbConnection database = new SqlConnection(Config.ConnectionString);
if (queryString["eliminar"] != null)
{
database.Delete<Prueba>(int.Parse(queryString["eliminar"]));
ViewData["notificacion"] = "Registro eliminado correctamente.";
}
return View();
}
// POST: /Default1/
[ActionName("Index")]
[HttpPost]
public ActionResult PostIndex(FormCollection form)
{
string cs = form["txtcod_sis"];
string co = form["txtcod_obra"];
string no = form["txtnom_obra"];
string ao = form["txtanyo_obra"];
IDbConnection DB = new SqlConnection(Config.ConnectionString);
Prueba p1 = new Prueba();
p1.CodObra = co;
p1.NomObra = no;
p1.AnyoObra = int.Parse(ao);
int? id = DB.Insert(p1);
ViewData["notificacion"] = "Registro agregado correctamente (ID " + id + ").";
IEnumerable<dynamic> captaciones = DB.Query("select * from prueba");
ViewData["captaciones"] = captaciones;
return View();
}
}
}
INDEX
Index PROOF FORM
@{
if (ViewData["notificacion"] != null)
{
<h4>@ViewData["notificacion"]</h4>
}
}
<div>
<form id="form1" method="post">
<fieldset>
<legend>Agregar</legend>
<table width="40">
<tr>
<td><label>Codigo Obra</label></td>
<td><label>Nombre Obra</label></td>
<td><label>Año Obra</label></td>
</tr>
<tr>
<td><input type="text" name="txtcod_obra" maxlength="10" /></td>
<td><input type="text" name="txtnom_obra" maxlength="10" /></td>
<td><input type="number" name="txtanyo_obra" min="1900" max="2050" maxlength="4" /></td>
</tr>
</table>
<p>
<input name="Button1" type="submit" value="enviar" />
</p>
</fieldset>
</form>
<fieldset>
<legend>Datos Captación</legend>
<table>
<tr>
<td><label>ID Obra</label></td>
<td><label>Código</label></td>
<td><label>Nombre</label></td>
<td><label>Año</label></td>
</tr>
@if (ViewData["captaciones"] != null)
{
IEnumerable<dynamic> captaciones = (IEnumerable<dynamic>)ViewData["captaciones"];
foreach (dynamic captacion in captaciones)
{
<tr>
<td><label>@captacion.id_sis</label></td>
<td><label>@captacion.cod_obra</label></td>
<td><label>@captacion.nom_obra</label></td>
<td><label>@captacion.anyo_obra</label></td>
<td><a href="[email protected]_sis"><small>Editar</small></a></td>
<td><a href="[email protected]_sis"><small>Eliminar</small></a></td>
</tr>
}
}
</table>
</fieldset>
</div>
Model
namespace WebApplication1.Models.NBI { [Table ("test")]
public class Prueba
{
[Key]
[Column("id_sis")]
public string IdSis { get; set; }
[Column("cod_obra")]
public string CodObra { get; set; }
[Column("nom_obra")]
public string NomObra { get; set; }
[Column("anyo_obra")]
public int? AnyoObra { get; set; }
}
}