I have object A which has a List<ObjectB>
ObjectB has several properties. Id
, Mandatory
, Name
etc.
So I return a viewmodel (ObjectA
) and have this in my razor:
@model ObjectA
<div>
<div>@Html.HiddenFor(m => ObjectA.ObjectC.ID)
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => ObjectA.ObjectC.Name)
</dt>
<dd>
@Html.DisplayFor(model => ObjectA.ObjectC.Name)
</dd>
</dl></div>
// display stuff from objectA
@using (Html.BeginForm())
{
foreach (var ft in ObjectA.ObjectB)
{
@Html.HiddenFor(c => ft.ID)
<div class="row">
<div class="col">
@if (ft.Mandatory)
{
@Html.CheckBoxFor(c => ft.Mandatory, new { id = ft.ID, disabled = "disabled" })
@Html.HiddenFor(c => ft.Mandatory)
}
else
{
@Html.CheckBoxFor(c => ft.Mandatory, new { id = ft.ID })
}
</div>
</div>
</div>
</div>
and in my Controller I tried as input parameter:
List<ObjectB> items
but it was null. Now I know I could try FormCollection
which I did and found out that form.Get("ft.id")
had the amount of items in de objectB list. same for mandatory. But I'd like it strong typed. Either:
1 object A with all subobjects of type B
2 a list/ienumerable of type objectB.
It's probably a small thing, but I can't see it right now.
edit my model:
public class ObjectA : BaseViewModel
{
public ObjectC DisplayOnly { get; internal set; }
public List<ObjectB> Features { get; set; }
}
My view: (see above) My controller:
[Route("Details/{id:int}")]
[HttpPost]
public ActionResult Details(ObjectA vm)
{
if (ModelState.IsValid)
{
int hid = Convert.ToInt32(RouteData.Values["id"]);
}
}