Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do i pass values from javascript to my MVC controller ?
Actually am trying to implement sign in with google and facebook in my application.I got some code and its working.But it is client side.
So now some javascript functions are there which will get called on successful login of google\facebook. I can get the logged in user name,image etc as well using the javascript.
But i want these info in my C# side.That is in my controller because i want to save these info in my local DB table.SO how can i pass these values to controller ?
I think i can use json to pass these value, but how ?
How can i get the image of the user?
Please help also let me know if there any other ways to do this sign using google\facebook using c# code instead of javascript.
Posted
Comments
F-ES Sitecore 22-Apr-15 14:04pm    
You can't log the client into a remote service via server code for security reasons. Maybe oauth is more what you're looking for if all you need is people to access *your* site via their facebook etc credentials.

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

http://www.codeproject.com/Articles/380635/Csharp-Application-Integration-with-Facebook-Twitt

1 solution

Most certainly, by "MVC" you mean not just the architectural pattern, but ASP.NET MVC Framework implementing it.

The way JavaScript communicates with server side is sending some HTTP requests via AJAX: http://en.wikipedia.org/wiki/Ajax_%28programming%29[^].

You can start here: https://msdn.microsoft.com/en-us/library/dd381533%28v=vs.100%29.aspx[^].

—SA
 
Share this answer
 
Comments
Am Gayathri 23-Apr-15 2:32am    
I tried the above msdn link. But still it is loading my page onclick of submit button.
View:

@Html.Encode(ViewData["Message"])
<p>
Page Rendered: @DateTime.Now.ToLongTimeString()
</p>
<span id="status">No Status</span>
<br />
@Ajax.ActionLink("Update Status", "GetStatus", new AjaxOptions{UpdateTargetId="status" })
<br /><br />
@using(Ajax.BeginForm("UpdateForm", new AjaxOptions{UpdateTargetId="textEntered"})) {
@Html.TextBox("textBox1","Enter text")
<input type="submit" value="Submit"/><br />
<span id="textEntered">Nothing Entered</span>
}


Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult About()
{
return View();
}
public string GetStatus()
{
return "Status OK at " + DateTime.Now.ToLongTimeString();
}

public string UpdateForm(string textBox1)
{
if (textBox1 != "Enter text")
{
return "You entered: \"" + textBox1.ToString() + "\" at " +
DateTime.Now.ToLongTimeString();
}

return String.Empty;
}
}

On click of submit it is loading the entire page.I want to load only that part asynchronously.

Many Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900