c# - Passing a javascript array to controller create action -
there 1 issue cannot find ideal solution i'm asking here.
how can pass array created in jquery view controller action create.
i have class model person
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;} }
the controller action create:
[httppost] [validateantiforgerytoken] public actionresult create([bind(include = "name,age")]person person) { if (modelstate.isvalid) { db.person.add(person); db.savechanges(); return view(configuration); }
the scenario create new person , add person list of friends. @ point figured create array of friends in jquery have no idea how post create action.
view of create:
@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) { (var = 0; < 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 list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
any appreciated. want pass friendsidarray
controller.
Comments
Post a Comment