1

我知道这个问题已经被问了无数次,但请原谅我,因为我现在浪费了很多时间,因此终于发布了。所以总结一下,我有一个视图模型,它有一些复杂的类型属性和一个复杂的类型列表属性。

public class ViewModel
{
    public ViewModel()
    {
        Gains = new List<Gain>();

    }
    [UIHint("Common")]
    public AllRegRecordsLog commonRegisterFields { get; set; }
    public Potential Potential { get; set; }
    [UIHint("Gains")]
    public List<Gain> Gains { get; set; }

    public void CreateGains(int count = 1)
    {

        for (int i = 0; i < count; i++)
        {

            Gains.Add(new Gain());

        }


    }

}

现在我的视图 createOrEdit 我将此属性称为,

@Html.EditorFor(model => model.Gains)

我在我的特定视图文件夹中放置了一个编辑器模板

@model Gains
    table id="gainsTable" class="content-tables"style="width:98%">
    <tr id="defaultGainsRow">
            <td class="editor-label">
                @Html.LabelFor(model => model.VolumeGainLow)
            </td>
            <td class="editor-field">
                @Html.TextBoxFor(model => model.VolumeGainLow)
                @Html.ValidationMessageFor(model => model.VolumeGainLow)
            </td>

            </tr>

     <tr>
    <td colspan="4">
      <input id="addGainsButton"type="button" class="t-button" Value="Add Potential Gains"/>
    </td>
    </tr>
    </table>

但不知何故,我收到错误“传递到字典中的模型项是'System.Collections.Generic.List'类型,但需要增益”

我正在使用 asp.Net MVC 4

请指出我正确的方向。

谢谢

比拉尔

4

3 回答 3

1

感谢您的帮助,我使用以下帖子解决了它

http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/

于 2012-09-03T18:51:35.023 回答
0

修改 editortemplate 以获取 List 并在其中执行循环逻辑

@model List<Gain>
@foreach(var gain in Model)
{
    table id="gainsTable" class="content-tables"style="width:98%">
    <tr id="defaultGainsRow">
            <td class="editor-label">
                @Html.LabelFor(gain=> gain.VolumeGainLow)
            </td>
            <td class="editor-field">
                @Html.TextBoxFor(gain=> gain.VolumeGainLow)
                @Html.ValidationMessageFor(gain=> gain.VolumeGainLow)
            </td>

            </tr>

     <tr>
    <td colspan="4">
      <input id="addGainsButton"type="button" class="t-button" Value="Add Potential Gains"/>
    </td>
    </tr>
    </table> 
}

编辑:我的剃须刀可能不是 100%,但你明白了

于 2012-08-30T06:01:30.033 回答
0

看起来您正在尝试将您的视图声明为绑定到属性而不是类型。这是不可能的。您必须将模型声明为 Web 项目引用的有效类型,模板才能正常工作。

Gains属性类型List<Gain> . Since yourGains view has aGains type declared, yourUIHintAttribute` 无效。

于 2012-08-30T06:02:50.360 回答