0

我在发布具有“复杂类型”模型的表单时遇到了一些问题:

我有一个模型:

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; }
    }

我的第一个问题是,在发布事件中,我的用户为 null .. 所以我在这里关注了一些帖子(例如MVC- 复杂类型上的模型绑定)使用for而不是foreach,但既然我这样做了,我的表格将不再发布:

看法:

      @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" />
        }

我的视图通过 ajax 呈现为部分加载(不确定它在这里有什么不同)。知道为什么它不会发布吗?如果我将所有 '[]' 像 'Users[0].FullName' 删除到 Users0.FullName 我会发布,但当然它不会被绑定。

谢谢你的帮助

以备不时之需进行编辑:操作:

[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) });

    }
4

1 回答 1

0

巴勃罗·罗密欧是对的,我添加了一个默认的 ctor 并且它起作用了。

于 2013-03-04T11:20:20.717 回答