Return a class in jQuery [closed]

-2

I have these fields hidden in my cshtml:

<input type="hidden" id="txtGeoTo" name="txtGeoTo" />
<input type="hidden" id="txtDateStart" name="txtDateStart" />
<input type="hidden" id="txtDateEnd" name="txtDateEnd" />

Properties:

public string txtOrigem { get; set; }
public string datIda { get; set; }
public string datVolta { get; set; }

How do I make this class direct in my jquery

function PegaHotelPacote() {

    $.ajax({
        url: '/Passo/PegaHotelPacote',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify({ aqui entra minha classe, acho}),
        sucess: function (data) {

        },
        error: function (error) {

        }
    });
}
    
asked by anonymous 25.03.2014 / 21:20

2 answers

1

Option 1:

You can use jQuery's own serialize method:

$.ajax({
    ...
    data: $('#form').serialize(),
    ...
});

HTML

<form id="form">
    <input type="hidden" id="txtGeoTo" name="txtGeoTo" />
    <input type="hidden" id="txtDateStart" name="txtDateStart" />
    <input type="hidden" id="txtDateEnd" name="txtDateEnd" />
</form>

Option 2:

Passing an object as a parameter:

$.ajax({
    ...
    data: {
           txtOrigem : $('#txtGeoTo').val(),
           datIda: $('#txtDateStart').val(),
           datVolta: $('#txtDateEnd').val()
    },
    ...
});

In both cases you just need to do this, .Net will map to the model / viewmodel passed as a parameter in Action.

    
25.03.2014 / 22:31
0

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 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

    
25.04.2014 / 06:28