0

Person と Address を含むエンティティ クラスを取得しました。

public class Person
{
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public List<Address> Addresses { get; set; }
}

public class Address
{
   public string ZipCode { get; set; }
   public string City { get; set; }
   public string State { get; set; }
   public string Country { get; set; }
}

私の見解では、いくつかのチェックボックスを表示しています。

@model DataContext.Models.Persons

@{
    ViewBag.Title = "Person list";
}

@using (Html.BeginForm("Update", "Test", FormMethod.Post))
{

    <div id="personContainer">
        @foreach(var t in Model.Person)
        {
            <input type="checkbox" value="@t.ID" name="@t.FirstName ">@t.FirstName <br />
        }
    </div>
    <p>
        <input type="submit" id="save">
    </p> 
}

私のコントローラーは次のようになります。

[HttpPost]
public JsonResult Update(Person p)
{

   return Json( new { redirectTo = Url.Action("Index") });
}

投稿したいデータは厳密に型指定されている必要があります。JSON を使用してデータ (この場合はすべてのチェックボックス) を「更新」コントローラーに戻すにはどうすればよいですか?

4

2 に答える 2

0

フォーム送信で実行するjqueryメソッドを設定して、シリアル化されたフォームを開始フォームのコントローラーに送信します

$('formSelector').submit(function () {
    if ($(this).valid()) {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.Success == true) {

                }
                else {
                    $('#Error').html(result.Html);
                }
            }
        });
    }
    return false;
});
于 2012-12-01T20:40:59.480 に答える