简体   繁体   中英

Send string from View to Controller ASP.Net mvc

So we are pretty new to ASP.Net MVC

We have this method that runs when we click on a button in our view. We would like to send the date string to our controller. How can we achieve this?

$('#calendar').fullCalendar({
        //weekends : false

        dayClick: function(date) {

            console.log(date.format());

        }
    })

This is our controller

   [HttpPost]
    public IActionResult Booking(string test)
    {
        var booking = new Booking();

        //booking.Date = dateTime;

        Console.WriteLine(test);

        var viewModel = new BookingsideViewModel
        {
            Subjects = new[] {"Matematik", "Dansk", "Engelsk"},
            Booking = booking
        };

        return View();

    }

you can do it using ajax call,

$.ajax({
        url: '@Url.Action("Booking")',
        data: { 'test' : date },
        type: "post",
        cache: false,
        success: function (response) {
            console.log("success");
        },
        error: function (error) {
            console.log(error.message);
        }
    });

you can also write url like this:

url:"/ControllerName/ActionName"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM