【问题标题】:Getting array data back into my viewmodel that contains an array将数组数据返回到包含数组的视图模型中
【发布时间】:2011-04-21 04:04:25
【问题描述】:

我正在使用 MVC3 并将 adminEditViewModel 视图模型传递给我的视图:

public class adminEditViewModel
{
    public Answer   Answer { get; set; }
    public string abc { get; set; }
}

类答案如下所示:

public class Answer
{
    public string[] ImageFile;
}

在视图中我使用这个:

@Html.EditorFor(model => @Model.Answer.ImageFile[@index])

当它在页面上时,它看起来像这样:

            <div class="editor-field">
                <input class="text-box single-line" id="Answer_ImageFile_0_" name="Answer.ImageFile[0]" type="text" value="" />
            </div>

当它以上面的视图模型作为参数返回到控制器时,我添加了一些数据,设置调试点,单击提交,然后看到视图数据中的 Answer 类包含 null,但其他内容(如 abc)已正确设置。

我的问题是我什至可以通过视图模型以正常方式从这样的数组中返回数据吗?

希望有好的建议

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    由于 asp.net mvc 提倡约定优于配置,生成的 html 必须采用某种标准格式才能与模型中的值列表或数组绑定。请看看这个question。答案包含指向steve senderson's post 的链接,该链接将列表值绑定到控制器操作参数。

    【讨论】:

      【解决方案2】:

      您的 Answer.ImageFile 是一个字段; ASP.NET MVC 仅绑定到属性。

      public class Answer
      {
          public string[] ImageFile { get ;set; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        相关资源
        最近更新 更多