2012-11-25 58 views
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; } 
    } 

我的第一個問題是,在事件後,我的用戶了空..所以我也跟着在這裏的幾個職位(如MVC- Model Binding on a Complex Type)用於代替的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(不知道它在這裏有什麼區別)。 任何想法爲什麼它不會發布?如果我刪除所有'[]'用戶[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) }); 

    } 
+0

你能分享生成的表單的HTML嗎? –

+1

您可能想要在UserInCircle上添加默認構造函數。我不認爲模型聯編程序能夠在沒有它的情況下實例化該類型。 –

+0

巴勃羅羅密歐是對的,我添加了一個默認ctor,它的工作。我不認爲我會獨自一人。謝謝! (ps巴勃羅羅密歐隨時可以把它作爲答案,所以我可以標記它解決給你信用:) :)) – Tom

回答

0

巴勃羅·羅密歐是正確的,我加了一個默認的構造函數,它的工作。