Pregunta

i have a script in my view which is:

$('.datepicker').datepicker
(
{ onSelect: function (dateText, inst) {
    //pass dateText to my controller

});
 </script>

my controller is like this:

    public ActionResult Index()
    {
        string dateSelected = dateText; //read dateText here
        if (DateTime.TryParse(dateSelected, out date))
        {
            date = Convert.ToDateTime(dateSelected);
            var reservations = db.Reservations.Where(r => r.Date == date).Include(r => r.Employee).Include(r => r.Room).OrderByDescending(r => r.Date);

            return View(reservations);
        }
        return View();

    }

i want dateText to be passed to the controller without the current page being refreshed or reloaded. how to do that?

i tried forms earlier and on the onselect event of the datepicker, i automatically submit the form so the controller can accept it. but i do not want the current page to be refreshed or reloaded.

i just want to pass dateText to the controller without the user noticing the passing.. some sort of $.post i guess but i dont know how to implement it..

UPDATE: here is my try, what is wrong:

ok here is my script:

     JSONstring = JSON.stringify(dateText);
    $.post("/Home/Index", { jsonData: JSONstring });

here is my controller:

     public ActionResult Index(string jsonData)
    {
        CacheClear();
        string dateSelected = jsonData;
         .....
        //i tried to debug it, but no value of jsonData

    }
¿Fue útil?

Solución

use ajax and send all the variables or data in the ajax data string and provide the url of your controller for example

    function add_to_cart(id , title, price,qty){

    $.ajax({
        type: "POST", 
   url: "your controller" + id ,
  data: "&title=" + title + "&price=" + price +"&quantity=" + qty ,
        complete: function(data) {
            //alert("Item added to cart");

        }
    });
   }

Otros consejos

i want dateText to be passed to the controller without the current page being refreshed or reloaded. how to do that?

You could use AJAX.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top