Hi 65sametkaya65,
Please refer below sample code.
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public JsonResult SaveLatLong(string latitude, string longitude)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Locations (latitude,longitude) VALUES (@latitude, @longitude)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@latitude", latitude);
cmd.Parameters.AddWithValue("@longitude", longitude);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
return Json("Inserted.");
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (p) {
$.ajax({
type: "POST",
url: "/Home/SaveLatLong",
data: '{latitude: "' + p.coords.latitude + '", longitude: "' + p.coords.longitude + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
};
});
</script>
</div>
</body>
</html>