HTML
複数の同じ名前のフィールドを含むフォームがあります。例えば:
<form action="" method="post" id="form">
<div class="itemsection" id="item1">
<input type="text" name="Price" />
<input type="text" name="Name" />
<input type="text" name="Catagory" />
</div>
<div class="itemsection" id="item2">
<input type="text" name="Price" />
<input type="text" name="Product" />
<input type="text" name="Catagory" />
</div>
<div class="itemsection" id="item3">
<input type="text" name="Price" />
<input type="text" name="Product" />
<input type="text" name="Catagory" />
</div>
</form>
サーバー側 (C#) には、アクション メソッドとモデル クラスがありProduct
ます。
//Action method accepts the form on server //It require the Product array
public ActionResult SaveItem(Product[] products)
{
.....
...
}
//Model class product
public Class Product
{
public int Id { get; set; }
public double Price { get; set; }
public string Name { get; set; }
public string Catagory { get; set; }
}
今私の質問は次のとおりですjquery $.ajax method
。Product
サーバー側では、アクション メソッドはクラス配列を受け入れます。フォームフィールドを配列に変換するにはどうすればよいですかJson
(製品配列に似ています)。私は試した:
$("#form").serialize();
&
$("#form").serializeArray();
1 つ目はすべてのフォーム フィールドの名前と値のペアを生成し、2 つ目はすべてのフォーム フィールドの名前と値の配列を生成します。そして私の要件は次のとおりです。
//currently my form contains 3 item section so :
[
{ "Price" : "value", "Name" : "value", "Catagory" : "value" },
{ "Price" : "value", "Name" : "value", "Catagory" : "value" }
{ "Price" : "value", "Name" : "value", "Catagory" : "value" }
]
またはを介してこれを達成する方法を誰か教えてもらえますJavaScript
かJQuery
?