I've come up with problem, when trying to fill data to my model. I have an "Resource" entity, which can have no-to-many "attributes". I have templates set up, which holds names for those attributes. When Resource is created, user chooses on of templates, then Attributes are created(empty) and program generates form for those attributes.

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })       
    @Html.HiddenFor(Model => Model.Resource)
    @for (int i = 0; i < Model.Attributes.Count(); i++)
        {
        <div class="form-group">
            @*Html.LabelFor(d => d.Attributes.ToArray()[i].Name, htmlAttributes: new { @class = "control-label col-md-2" })*@
            <h4>@Html.Raw(Model.Attributes.ToList()[i].Name)</h4>
            <div class="col-md-10">
                @Html.TextBoxFor(Model => Model.Attributes.ToList()[i].Value, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(Model => Model.Attributes.ToList()[i].Value, "", new { @class = "text-danger" })
            </div>
        </div>
    }
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

This form uses this View model:

public class ResourceAttributesViewModel
{
    public virtual Resource Resource { get; set; }
    public virtual ICollection<_Attribute> Attributes { get; set; }
}

problem is that when i hit "submit" button, it gives me view model with null Resource and Attributes properties

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Fill(ResourceAttributesViewModel AttributeSet)
    {
        if(ModelState.IsValid)
        {
            foreach (var attr in AttributeSet.Attributes)
            {
                db.Entry(attr).State = EntityState.Modified;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        return View(AttributeSet);
    }

if it helps, there is POST string sent by browser

__RequestVerificationToken=XoVM9h_3njX5x2m35b_vKKHY3m5UDaYm9_2ZMfNkglouqHJCSw2NO56Tv2Sb3kXy8qC8XBLXawoQv0ft0xc-LxYmQGfi4EAqroq2b63Wb9Q1&Resource=System.Data.Entity.DynamicProxies.Resource_7639327FA0332BEBC7FB6836F70C3D62C3D744D76F2C3F8DDFCE679AA8CA31DC&%5B0%5D.Value=100&%5B1%5D.Value=200

Related posts

Recent Viewed