Question

I have some problem posting a form with 'complex type' model:

I have a Model:

public class CircleEditViewModel
    {
        [Key]
        public int CircleId { get; set; }

        [Required]
        [MaxLength(100)]
        public string Name { get; set; }

        public bool IsSystem { get; set; }
        public class UserInCircle
        {
            public UserInCircle(User user)
            {
                this.UserId = user.UserId;
                FullName = user.FullName;

            }
            public int UserId { get; set; }
            public byte[] Picture { get; set; }
            public string FullName { get; set; }
            public bool isInCircle { get; set; }
        }
          public List<UserInCircle> Users { get; set; }
    }

My first problem was that at post event, my Users where null.. so i followed a few posts on here (like MVC- Model Binding on a Complex Type) to use a for instead of a foreach,but since i did so, my form won't post anymore:

View:

      @model Wims.Website.ViewModels.CircleEditViewModel
        <script type="text/javascript">
            $(document).ready(function () {

                $.validator.unobtrusive.parse('form');
            });


        </script>
@using (Ajax.BeginForm(Html.ViewContext.RouteData.Values["Action"].ToString(), null, new AjaxOptions { HttpMethod = "POST", OnSuccess = "SaveDone(data)" }, new { id = "editform" }))
{
    @Html.ValidationSummary(true)

            <fieldset>
                <legend>Circle</legend>
                @Html.Label(DateTime.Now.ToString());
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
            </fieldset>

            if (Model.Users != null)
            {
                for (int i = 0; i < Model.Users.Count; i++)
                {            
                    <div class="userDetail">
                        <div>
                            <div>
                                @Html.DisplayFor(model => Model.Users[i].isInCircle);
                            </div>
                            <div class="iconDiv">
                                @Html.Image("~/Content/Images/defaultUser.jpg", Model.Users[i].FullName, null);
                            </div>
                            <div>     
                                 @Html.TextBoxFor(model => Model.Users[i].FullName)
                                 @Html.HiddenFor(model => Model.Users[i].UserId)

                            </div>
                        </div>

                    </div>
                    <div style="clear: both"></div>
                }
            }
         @Html.GenerateSecureDataControls(model => model.CircleId)
            <input type="submit" value="Save" />
        }

My view is rendered as a partial loaded thru ajax (not sure it makes any difference here). Any idea why it won't post? If i remove all the '[]' like 'Users[0].FullName' to Users0.FullName i will post, but of course it won't be bound.

Thanks for your help

Edit just in case needed: Action:

[HttpPost]
    public ActionResult Edit(CircleEditViewModel circleData, FormCollection collection)
    {
        if (ModelState.IsValid)
        {
            using (var logic = new CircleLogic())
            {
                Circle circle = logic.GetCircleById(circleData.CircleId, WebMatrix.WebData.WebSecurity.CurrentUserId);
                if (circle == null)
                {
                    return HttpNotFound();
                }
                else
                {

                    circle.Name = circleData.Name;
                    logic.UpdateCircle(circle, GetSelectedUser(collection));

                }
                return PartialView("_CircleAndUsers", GetData(logic, circle.CircleId));
            }
        }

        return this.Json(new { success = false, viewdata = RenderRazorViewToString("_CircleAndUsers", circleData) });

    }
Was it helpful?

Solution

Pablo Romeo was right, i added a default ctor and it worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top