2011-01-17 11 views
0

私のビューには、次の2つのhtml要素(これらの要素はJQueryによって動的に作成され、モデルを使用してビューにバインドされていません)を持つフォームがあります。JQueryで作成した要素をコントローラのパラメータにバインドする

public class JsonCommand 
{ 
    public string NickName { get; set; } 
} 

I以下のコントローラを持っています:

<input id="JsonCommand_0__NickName" type"radio" name="JsonCommand[0].NickName" value="name1" checked> 
<input id="JsonCommand_1__NickName" type"radio" name="JsonCommand[1].NickName" value="name2" checked> 

は、私は以下のクラスを持って、私はこのコントローラに投稿するJQuery.Formプラグインを使用しています

[HttpPost, Authorize, Compress] 
public ActionResult Edit(IEnumerable<JsonCommand> command) 
{ 
    .... 
} 

。この方法でフォームコレクションをJsonCommandオブジェクトにシリアル化することは可能ですか?現在私がしようとすると、コマンドのためのnull値を取得します。

クライアント側でコレクションを作成してJsonCommandオブジェクトにバインドする方法はありますか?

おかげ

答えて

0

私のために完全に罰金、次の作品:

モデル:

public class JsonCommand 
{ 
    public string NickName { get; set; } 
} 

コントローラー:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(new[] 
     { 
      new JsonCommand { NickName = "name1" }, 
      new JsonCommand { NickName = "name2" }, 
     }); 
    } 

    [HttpPost] 
    public ActionResult Index(IEnumerable<JsonCommand> command) 
    { 
     return Json(new { message = "ok" }); 
    } 
} 

ビュー(~/Views/Home/Index.aspx):

<script type="text/javascript" src="https://github.com/malsup/form/raw/master/jquery.form.js?v2.43"></script> 
<script type="text/javascript"> 
    $('form').ajaxForm(function (result) { 
     alert(result.message); 
    }); 
</script> 

<% using (Html.BeginForm()) } %> 
    <%: Html.EditorForModel() %> 
    <input type="submit" value="OK" /> 
<% } %> 

エディタのテンプレート(~/Views/Home/EditorTemplates/JsonCommand.aspx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.JsonCommand>" %> 
<%: Html.RadioButtonFor(x => x.NickName, Model.NickName) %> 

はまた、ラジオボタンは通常、ブール値の代わりの文字列にバインドされていることに注意してください。

関連する問題