Loading Javascript array into asp.net MVC controller

I'm currently trying to develop a web application in which the user defines a set of points with coordinates on Google maps, these points are stored in an array like this:

 mvcPolygon: new google.maps.MVCArray(),

Now I need a username, password, etc. and sends it to the server. I am new to javascript and frontend development, so I have absolutely no idea how to do this. So I currently have javascript code that stores the coordinates and what I need is that when the user finishes defining his polygon and filling out a form with username, password, email, etc., all this information will be sent to the controller as soon as the user clicks the submit button. The web application is currently under development on ASP.NET MVC 5 and C #, any help would be really appreciated!

0
source share
2 answers

:

  • , . , , .. html , MVC ( ).

  • jQuery ajax javascript, , MVC (. , ).

, , , cookie , , ( , , ).

2:

JavaScript:

var myobject = {
    username: "Joe",
    password: "password",
    points: [{ x: 111.1112, y: 22.2222 },{ x: 11.12, y: 21.11 }]
}

jQuery.ajax({
    url: "/MyController/MyAction",
    contentType: "application/json",
    dataType: "json",
    data: JSON.stringify(myobject)
})

#:

public class PointModel 
{
    public double x { get; set; }
    public double y { get; set; }
}

public class MyModel
{
    public string username { get; set; }
    public string password { get; set; }
    public IList<PointModel> points { get; set; }
}

public class MyController : Controller 
{
    [HttpPost]
    public JsonResult MyAction(MyModel model) {
        ...
    }
}
+3

java script JavaScript

<script type="text/javascript">
 var array = [];
    array.push(10);
    array.push(20);
</script>

Ajax

 var _data = {
            'array ': array 
        };
$.Post(url ,_data, function(data){
})
+1

Source: https://habr.com/ru/post/1525593/


All Articles