c# - Send dynamic list of strings to controller from view in MVC -


i have view this:

<div class="editor-label">     @html.labelfor(model => model.parts) </div> <div class="editor-field">     <ul id="parts"></ul>     <span id="parts-button" onclick="addpart()">+ add part</span> </div> 

, js so:

function addpart(name) {     var input = $('<input>').attr("name", "parts");      if (name != null)         input.val(name);      $('<li>')         .append(input)         .append($('<div>')             .html("x")             .on("click", function () {                 var div = $(this)                 div.parent().slideup(function () { $(this).remove(); });             })             .addclass("delete"))         .appendto($('#parts')).hide().slidedown(); } 

and model this:

public virtual ilist<string> parts { get; set; } 

my question is; it's possible populate models list of strings directly dynamic number of elements in view. or have following:

public actionresult create(model model, ilist<string> parts) {     model.parts = parts; 

any or link relevant information highly appreciated. have been searching, unable find relevant information sort of problem.

thanks questions answer: generate list in view , pass controller figured out if change name of input include , index ([i]) passes string correctly.

solution:

while creating input changed javascript code following:

var input = $('<input>').attr("name", "parts[" + index + "]"); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -