2012-04-03 3 views
0

サーバー:デフォルトのモデルバインダーはIEnumerable <FormItem>を生成できますか?

public class FormItems 
{ 
    public IEnumerable<MyClass> Values { get; set; } 
} 

がクライアント:

<form id="myform" action="/" method="post"> 
    <!-- Those inputs could be added dynamically --> 
    <input type="text" name="[0].Value" /> 
    <input type="text" name="[1].Value" /> 
    <input type="text" name="[2].Value" /> 
    <input type="text" name="[3].Value" /> 

    <button type="submit">OK</button> 
</form> 

、最終的には、フォームをAJAXify:

$(function() { 
    $('#myform').submit(function() { 
     var form = $(this); 
     $.ajax({ 
      url: form.attr('action'), 
      type: form.attr('method'), 
      data: form.serialize(), 
      success: function(result) { 

      } 
     }); 
    }); 
}); 

は、どのように私は強く型付けされたのIEnumerableにAjaxデータを取得し、デフォルトのモデルバインダーを使用することができますか? MyClassを想定し

[HttpPost] 
public JsonResult Save(FormItems data) 
+0

(各値の入力の名前がFormItemsに列挙プロパティ名を照合することによって前置されていることに注意)、次のようになります。このような何か

public class MyClass { public string Value { get; set; } } 

ですそれは、Viewでどれくらい正確に構造化されているかによって異なります。あなたのビューからHTMLを投稿できますか? – mattytommo

答えて

1

は、あなたのHTMLが

< form id="myform" action="/" method="post"> 
    <!-- Those inputs could be added dynamically --> 
    <input type="text" name="Values[0].Value" /> 
    <input type="text" name="Values[1].Value" /> 
    <input type="text" name="Values[2].Value" /> 
    <input type="text" name="Values[3].Value" /> 

    <button type="submit">OK</button> 
</form> 
関連する問題