Michael
Michael

Reputation: 13616

Pass data from view razor page to action method in controller

I need to pass data from this razor page view:

enter image description here

To this action method:

enter image description here

I want my data being sent to an action on page load.

How to implement this?

Upvotes: 0

Views: 807

Answers (1)

Andrei
Andrei

Reputation: 44600

I would use Ajax & jQuery to push data to the server:

Javascript

$(function() { // on page load
    $.post({ //do ajax post request
        url: '/OauthCallBack/GmailOAuthCallback',
        data: 'code=' + someCode, //your data 
        success: function () {
            //Success callback if necessary
        }
    })
});

Controller

public class OauthCallBackController : Controller
{
    [HttpPost]
    public ActionResult GmailOAuthCallback(string code)
    {
        //Do something

        return new EmptyResult(); //or return anything :)
    }
}

Upvotes: 2

Related Questions