Solution
Create a Model class
public class Modelo
{
public string txtOrigem { get; set; }
public DateTime datIda { get; set; }
public DateTime datVolta { get; set; }
}
Create Your Control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class ModelosController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult PostModelo(MvcApplication1.Models.Modelo modelo)
{
return Json(modelo, JsonRequestBehavior.DenyGet);
}
}
}
Create a View from the control like this:
@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.8.2.intellisense.js"></script>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script>
$(document).ready(function (e) {
$("#ButEnviar").click(function () {
var dados = "txtOrigem=" + $("#txtGeoTo").val()
dados = dados + "&datIda=" + $("#txtDateStart").val();
dados = dados + "&datVolta=" + $("#txtDateEnd").val();
$.post("/Modelos/PostModelo", dados, function (data) {
alert(data.txtOrigem + "\n" + data.datIda + "\n" + data.datVolta)
}, 'json');
});
});
</script>
</head>
<body>
<div>
<input type="hidden" id="txtGeoTo" name="txtGeoTo" value="Origem 1" />
<input type="hidden" id="txtDateStart" name="txtDateStart" value="10/10/2010" />
<input type="hidden" id="txtDateEnd" name="txtDateEnd" value="12/10/2010" />
<button type="button" id="ButEnviar">Enviar</button>
</div>
</body>
</html>
As long as you click Send it will execute Jquery.post , which is in $("#ButEnviar")
event click
, it will fetch the information in the hidden
fields, and send via ajax for the controller method ( /Modelos/PostModelo
), as shown in the figure below.
Afterprocessingthemethod,itresendsthesamedataagainforView
But you can make the best use of it.
Note: I put the datIda
and datVolta
as DateTime