JavaScript数组发送给ASP.NET MVC控制器 [英] Javascript array sending to asp.net MVC controller

查看:148
本文介绍了JavaScript数组发送给ASP.NET MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试图建立一个Web应用程序中,用户定义了对谷歌地图的坐标点的集合,这些点都存储像这样的一个阵列上:

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

 mvcPolygon: new google.maps.MVCArray(),

现在我需要的用户名,密码等,并将其提交给服务器。我是pretty新的JavaScript和前端开发,所以我很无能这里,什么是要走的路。
所以目前我的JavaScript code这是存储的坐标和我需要的是,当用户finnished定义自己的多边形和填充用户名,密码,电子邮件等形式的所有这些信息被发送到控制器只要用户点击提交按钮。
该web应用目前正在对ASP.NET MVC 5和C#开发的,任何帮助将是非常美联社preciated!

Now I need username, password, etc. and submits it to the server. I'm pretty new with javascript and frontend development so I'm quite clueless here as to what is the way to go. So currently I have the javascript code which is storing the coordinates and what I need is that when the user is finnished defining his polygon, and filling a form with username, password, email, etc. all this info gets sent to the controller as soon as the user hits the submit button. The webapplication is currently being developed on ASP.NET MVC 5 and c# , any help would be really appreciated!

推荐答案

有本质上是2种方式,你可以得到这些数据返回给服务器:

There's essentially 2 ways you can get that data back to the server:


  1. 您可以将数据放入一个表单元素和后形成到的设置为接收它的动作。这就需要你把你的用户名,密码等成系列的HTML元素与名称属性设置遵循MVC的约定(的此页面对公约的一些信息使用)。

  1. You can put the data into a form element and post that form to an action that's set up to receive it. This will require you to put your username, password, etc into a series of html elements with name attributes set up to follow MVC's conventions (This page has some information on the conventions to use).

您可以使用jQuery的Ajax功能来发送包含您的数据的原始JavaScript对象到一个MVC操作(请参见这个答案,它的后挂关于如何做到这一点)的详细信息。

You can use jQuery's ajax features to send a raw javascript object containing your data to an MVC action (See this answer and it's linked post for details on how to accomplish this).

在这两种情况下,你可能不应该发送用户名和放大器;密码每个请求的服务器,你可以使用一个身份验证cookie来验证一旦他们登录到你的应用程序用户的身份(有足够的资源在网上解释这是如何工作)。

In either case, you probably shouldn't be sending a username & password to the server with every request, as you can use an authentication cookie to verify a user's identity once they're logged in to your application (there's plenty of resources online explaining how this works).

对于选择2:

使用Javascript:

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)
})

C#:

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) {
        ...
    }
}

这篇关于JavaScript数组发送给ASP.NET MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆