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

时间:2014-11-27 09:06:58

标签: c# jquery asp.net-mvc-4 razor

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

如何将jQuery中创建的数组从view传递到controller action 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;}
}

控制器动作创建:

[HttpPost] 
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Name,Age")]Person person)
{
    if (ModelState.IsValid)
    {
        db.Person.Add(person);
        db.SaveChanges();
        return View(configuration);
    }

场景是我创建了一个新人并向该人添加了一个朋友列表。在这一点上,我想我在jQuery中创建了一个朋友数组,但不知道如何将其发布到创建操作中。

创建视图:

@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传递给控制器​​。

0 个答案:

没有答案