c# - Passing array form to controller -


how can send array of model html view controller. in code receive null on controller parameter.

public class propmodel {     public string name { get; set; }     public string value { get; set; } }  [httppost] public actionresult index(propmodel[] model) {         foreach (var r in model)         {             //do somethings         }         return view(); } 

view:

@{     layout = null; }  <!doctype html>  <html> <head>     <title></title> </head> <body>     <div>          @using (html.beginform())         {             (int = 0; < 3; i++)             {                 @html.textbox("name")                 @html.textbox("value")             <br/>             }             <input type="submit" value="submit"/>         }     </div> </body> </html> 

you can use editortemplates handle situation.

let's make new viewmodel our view.

public class proplistvm {     public list<propmodel> props { set; get; }     public proplistvm()     {         props = new list<propmodel>();     } } 

now in our get action, create object of new viewmodel, set props collection , send our view.

public actionresult index() {                var vm = new proplistvm { props = getpropsfromsomewhere() };     return view(vm); } private list<propmodel> getpropsfromsomewhere() {     var list = new list<propmodel>();     //hard coded demo. may replace db items     list.add(new propmodel { name = "detroit", value = "123" });     list.add(new propmodel { name = "ann arbor", value = "345" });     return list; } 

now let's create editortemplates. go ~/views/yourcontrollername , create folder called "editortemplates" , create new view there same name of property name(propmodel.cshtml)

enter image description here

add code new editor template.

@model replaceyournamespacehere.propmodel <div>     name :@html.textboxfor(s=>s.name) <br />     value : @html.textboxfor(s=>s.value) </div> 

make sure replace replaceyournamespacehere actual namespace propmodel class available.

now let's go our original view(index.cshtml) typed instance of our proplistvm viewmodel.

@model replaceyournamespacehere.proplistvm @using (html.beginform()) { <div>     @html.editorfor(s=>s.props)             <input type="submit" value="submit"/> </div> } 

now let's have action method handle form posting, have parameter of type proplistvm , mvc model binding bind properties of objects form posted. can check props property of object , loop through each items.

[httppost] public actionresult index(proplistvm model) {     foreach (var prop in model.props)     {         string s = prop.name;         //do cool somethings     }     // return or redirect     //if returning posted model form,      //reload props property.     model.props = getpropsfromsomewhere();     return view(model); } 

and result ??? profit !!!!!!

in example, sent 2 propmodel items name , value properties set, if want empty form, add propmodel objects without initializing property values. enter image description here


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -