c# - How can I pass an input/Textbox Field to Controller? ASP.NET MVC and Entity Framework -


i've form , simple model following input fields:

public class order {     public int id { get; set; }     public datetime orderdate { get; set; }     public string article { get; set; }     public double price { get; set; }     public string notice { get; set; } } 

my form looks this

enter image description here

i can catch values on controller

[httppost]         [validateantiforgerytoken]         public actionresult create([bind(include = "orderid,orderdate,article,price,notice")] order order)         {             if (modelstate.isvalid)             {                 db.orders.add(order);                 db.savechanges();                 return redirecttoaction("index");             }             return view(order);         } 

so. want add dynamic fields jquery. works fine don't know how values multiple input fields. , that's problem.

so want create each order entity. (how in last image)

please take @ last image

enter image description here

that's result want have! how can resulut in asp.net mvc c#? work entity framework 6 thank help!

enter image description here

first need change actions accept list<order> parameters (or typed viewmodel property of list<order> type):

 [httpget]  public actionresult create()  {               var model = new list<order>();      return view(model);  }   [httppost]  [validateantiforgerytoken]  public actionresult create(list<order> orders)  {      // update code omitted      return view(order);  } 

then in view add model declaration:

@model list<order>; 

now part need correct naming of dynamically added order objects on html input names keep model binder happy.

when creating first order item on input names should be:

name="[0].id" name="[0].orderdate" name="[0].article" name="[0].price" name="[0].notice" 

this first order , on etc:

name="[1].id" name="[1].orderdate" name="[1].article" name="[1].price" name="[1].notice" 

this article worth read:

model binding list


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -