视图模型的列表是空的行动 [英] ViewModel's list is null in action

查看:105
本文介绍了视图模型的列表是空的行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的第一个ASP.NET MVC 3应用程序,我有一个观点,即是这样的:

I'm working on my first ASP.NET MVC 3 application and I've got a View that looks like this:

@model IceCream.ViewModels.Note.NotesViewModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.TextBoxFor(m => m.Name)

    foreach (var item in Model.Notes)
    {
        @Html.EditorFor(m => item);
    }

    <input type="submit" value="Submit"/>
}

和我有一个看起来像这样的EditorTemplate:

And I have an EditorTemplate that looks like this:

@model IceCream.ViewModels.Note.NoteViewModel
<div>
    @Html.HiddenFor(m => m.NoteID)
    @Html.TextBoxFor(m => m.NoteText)
    @Html.CheckBoxFor(m => m.IsChecked)
</div>

NotesViewModel看起来像这样:

NotesViewModel looks like so:

    public class NotesViewModel
    {
        public string Name { get; set; }
        public IEnumerable<NoteViewModel> Notes { get; set; }
    }

NoteViewModel看起来是这样的:

NoteViewModel looks like this:

public class NoteViewModel
{
    public int NoteID { get; set; }
    public System.DateTime Timestamp { get; set; }
    public string NoteText { get; set; }
    public bool IsChecked { get; set; }
}

当它被传递给视图的NotesViewModel填充就好了。然而当点击提交按钮时,控制器动作处理后只对视图模型的名称属性的值。票据财产 - 的已签笔记/用户选中列表 - 为空。我有那些TextBoxFor和CheckBoxFor元素的填充之间的脱节显示视图时和视图模型被送回。在这个指导?

The NotesViewModel is populated just fine when it is passed to the view. However when the submit button is clicked, the controller action handling the post has only the value for the Name property of the viewmodel. The Notes property - the list of notes that have been checked/unchecked by the user - is null. I've got a disconnect between the populating of those TextBoxFor and CheckBoxFor elements when the view is displayed and the ViewModel being sent back. Guidance on this?

SOLUTION
要感谢空军终于男人直设置我在此。据我了解,从根本上改变我的循环

SOLUTION Thanks go to Mystere Man for setting me straight on this. As I understand it, essentially by changing my loop to

@ Html.EditorFor(M = GT; m.Notes)

改变底层的HTML,我的理解提供了合适的型号上后绑定。纵观生成的HTML,我知道我得到以下的注意事项之一发生:

changes the underlying HTML, which I understand provides for the proper model binding on the post. Looking at the resulting HTML, I see that I get the following generated for one of the Notes:

<div>
  <input id="Notes_0__NoteId" type="hidden" value="1" name="Notes[0].NoteId">
  <input id="Notes_0__NoteText" type="text" value="Texture of dessert was good." name="Notes[0].NoteText">
  <input id="Notes_0__IsChecked" type="checkbox" value="true" name="Notes[0].IsChecked>
</div>

这比我原来的code生成此HTML不同:

Which is different than this HTML generated by my original code:

<div>
   <input id="item_NoteId" type="hidden" value="1" name="item.NoteId>
   <input id="item_NoteText" type="text" value="Texture of dessert was good." name="item.NoteText" >
   <input id="item_IsChecked" type="checkbox" value="true" name="item.IsChecked">
</div>

通过注释循环,生成的HTML基本上失去任何引用到视图模型的注释财产,而HTML被正确填充,复选框值的设置已经没有办法自己的价值观传达回视图模型,我猜是模型结合的点。

By looping through the Notes, the generated HTML essentially loses any references to the viewmodel's Notes property and while the HTML gets populated correctly, the setting of the checkbox values has no way to communicate their values back to the viewmodel, which I guess is the point of the model binding.

所以,我学到了一些东西,这是很好的。

So I learned something, which is good.

推荐答案

你是个聪明的家伙,所以看你的看法。然后,考虑HTML被如何生成的。然后,考虑模型绑定是如何在回发应该知道的基础上生成的HTML重新填写注意事项。

You're a smart guy, so look at your view. Then, consider how the HTML gets generated. Then, consider how on postback the Model Binder is supposed to know to re-populate Notes based on the generated HTML.

我想你会发现,你的HTML没有在它的模型绑定足够的信息来看着办吧。

I think you'll find that your HTML doesn't have enough information in it for the Model Binder to figure it out.

考虑一下:

@EditorFor(m => Model.Notes)

而不是为循环,你基本上是隐藏在EditorFor功能的上下文。

Rather than the for loop where you are basically hiding the context from the EditorFor function.

这篇关于视图模型的列表是空的行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆