Solution
Create a Template class
public class Modelo
{
public string txtOrigem { get; set; }
public DateTime datIda { get; set; }
public DateTime datVolta { get; set; }
}
Create Your Controller
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 controller 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>
When you click Submit it will run the Jquery.post, who is in $("#ButEnviar")
event click
, it will fetch the information in the fields of the type hidden
, and send via ajax for the controller method (/Modelos/PostModelo
), as shown in the figure below.
After processing the method it re-sends the same data to View
But you can use it in the best possible way.
Note: I put the datIda
and datVolta
as Datetime
I did not understand the question. Please re-read the question, because it is not clear.
– Miguel Angelo
I serialized like this and I will test it. I got the question wrong. result = jQuery.parseJSON('{"txtDestino": "' + $("#txtGeoTo"). val() + '" , "datIda": "' + $("#txtDateStart"). val() + '", "datVolta": "' + $("#txtDateEnd"). val() + '", "intAdultos": "' + $("#txtAdulto"). val() + '" }'); $.ajax({ url: '/Step/Pegahotelpackage', dataType: "json", contenttype: "application/json; charset=utf-8", type: "POST", date: JSON.stringify({_hotel: result}), sucess: Function (date) { },
– pnet
You could edit the question with the new information as well as comment gets very confusing.
– Miguel Angelo