モデルを試してください:
public class Car
{
public int Id { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public DateTime Year { get; set; }
public List<Passanger> Passangers { get; set; }
}
public class Passanger
{
public int Id { get; set; }
public string Name { get; set; }
}
見る:
@model Car
@using(Html.BeginForm("actionName", "controllerName", FormMethod.Post, new { id = "my-form" }))
{
@Html.HiddenFor(x => x.Id)
@Html.TextBoxFor(x => x.Manufacturer)
@Html.TextBoxFor(x => x.Model)
@Html.TextBoxFor(x => x.Year)
for(int i = 0, i < Model.Passangers.Count(), i++)
{
@Html.HiddenFor(x => Model.Passangers[i].Id)
@Html.HiddenFor(x => Model.Passangers[i].Name)
}
<input type="button" value="Submit" id="form-submit" />
}
<script type="text/javascript">
$(document).on('click', '#form-submit', function(){
$.ajax({
url: "Car/AddCar",
type: "POST",
data: $('form#my-form').serialize(),
success: function (data)
{
alert(data);
// put the data in the container where you have the form.
}
});
});
</script>
もっと