C# 将javascript数组传递给控制器创建操作

C# 将javascript数组传递给控制器创建操作,c#,jquery,asp.net-mvc-4,razor,C#,Jquery,Asp.net Mvc 4,Razor,有一个问题我找不到理想的解决方案,所以我在这里问 如何将jQuery中创建的数组从视图传递到控制器操作create中 我有一个班级模范人物 public class Person { public int personId {get; set;} public string Name {get; set;} public int Age {get; set;} public virtual ICollection <Friend> Friends {g

有一个问题我找不到理想的解决方案,所以我在这里问

如何将jQuery中创建的数组从视图传递到控制器操作create中

我有一个班级模范人物

public class Person
{
    public int personId {get; set;}
    public string Name {get; set;}
    public int Age {get; set;}
    public virtual ICollection <Friend> Friends {get; set;}
}
场景是,我创建一个新的人,并向该人添加一个朋友列表。此时,我想我在jQuery中创建了一个朋友数组,但不知道如何将其发布到createaction中

创建视图:

@using Life.models
@model Life.models.Person
@{
ViewBag.Title = "Create person";
}
<script src="~/Scripts/jquery-2.1.1.min.js"></script>

<script>
$(function () {
    var FriendsIdArray = [];
    var FriendsTxtArray = [];
    $('#chooseFriendClick').click(function () {
        if ($('#friends :selected').text() != "---Select---") {
            var Id = $('#friends').val();
            var Txt = $('#friends :selected').text();
            var test = ifExist(FriendsIdArray, Id);
            if (test != null) {
                FriendsIdArray.splice(test, 1);
                FriendsTxtArray.splice(test, 1);
            } else {
                FriendsIdArray.push(Id);
                FriendsTxtArray.push(Txt);
            }
            $('#FriendsList').text(FriendsTxtArray);
        }

        function ifExist(arr, obj) {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] == obj)
                    return i;
            }
        }
    });
</script>
@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Person</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-  2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
    <div class="form-group">
    @Html.DropDownList("friends", new SelectList(dbContext.Friends, "FriendId", "Name"), "---Select---", new { @class = "form-control" })
    <a id="chooseFriendClick" href="#">Choose Friend</a>
    @Html.Label("Choosen Friends:", new { @class = "control-label col-md-2" }) <text  id="FriendsList" class="control-label col-md-1"></text>
  </div> 
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

任何帮助都将不胜感激。我想将FriendsIdArray传递给控制器。

您应该阅读此内容;我这样做是为了显示我创建的人已经选择的朋友,当我从列表中选择一个朋友并单击“选择朋友”时,该脚本会以这种方式工作,它会将该朋友id添加到FirendsIdArray,并向FirendsTxtArray添加名称,如果我选择已经在数组中的朋友,它会将他从列表中删除。
@using Life.models
@model Life.models.Person
@{
ViewBag.Title = "Create person";
}
<script src="~/Scripts/jquery-2.1.1.min.js"></script>

<script>
$(function () {
    var FriendsIdArray = [];
    var FriendsTxtArray = [];
    $('#chooseFriendClick').click(function () {
        if ($('#friends :selected').text() != "---Select---") {
            var Id = $('#friends').val();
            var Txt = $('#friends :selected').text();
            var test = ifExist(FriendsIdArray, Id);
            if (test != null) {
                FriendsIdArray.splice(test, 1);
                FriendsTxtArray.splice(test, 1);
            } else {
                FriendsIdArray.push(Id);
                FriendsTxtArray.push(Txt);
            }
            $('#FriendsList').text(FriendsTxtArray);
        }

        function ifExist(arr, obj) {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] == obj)
                    return i;
            }
        }
    });
</script>
@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Person</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-  2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
    <div class="form-group">
    @Html.DropDownList("friends", new SelectList(dbContext.Friends, "FriendId", "Name"), "---Select---", new { @class = "form-control" })
    <a id="chooseFriendClick" href="#">Choose Friend</a>
    @Html.Label("Choosen Friends:", new { @class = "control-label col-md-2" }) <text  id="FriendsList" class="control-label col-md-1"></text>
  </div> 
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}