asp.net mvc没有反序列化整个子列表

时间:2013-12-10 22:56:40

标签: asp.net-mvc razor deserialization

我的ASP.NET MVC应用程序没有反序列化整个列表,而是只有6个注释,给出了下面的简略示例。可能是什么原因造成的?在页面上,我看到了正确的结果数量。

[HttpGet]
public ActionResult Notes() {
    var viewModel = new NotesViewModel();   
    viewModel.Notes = GetNotes(); // returns 50+ notes, displaying as expected
    return View(viewModel);
}

[HttpPost]
public ActionResult Notes(NotesViewModel viewModel) {
  // viewModel.Notes only has 6 notes, but 50+ were passed to the view above
}

public class NotesViewModel {
  public List<Note> Notes { get; set; }
}

public class Note {
  public long Id { get; set; }
  public string Text { get; set; }
}

<table>
@for (int i = 0;i < Model.Notes.Count;i++) {                                
    var note = Model.Notes[i];
    <tr>
        <td>@note.Id</td>
        <td>@Html.TextBoxFor(m => m.Notes[i].Text)</td>                                                                                  
    </tr>
}     
</table>
<input type="submit" value="Save" />

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。发生这种情况的原因是因为在Id的列表中有一个跳过的数字(故意)导致迭代器停止循环。您需要特定的索引列表(foreach不起作用)。

但是我有条不紊地跳过了显示项目,而这正在打破数组索引的连续性。