【问题标题】:Empty ViewModel returned from view从视图返回的空 ViewModel
【发布时间】:2012-07-02 19:10:32
【问题描述】:

我正在将 ASP.NET MVC 4 用于网站,我正在做的事情之一是 CSV 导入。我想将 CSV 列映射到 ORM 的属性上。文件上传后,我想显示列号,第一行(包含csv文件中的标题)和第二行(数据预览)。

/// <summary>
/// The mapping view model.
/// </summary>
public class MappingViewModel
{
    #region Public Properties

    /// <summary>
    /// Gets or sets the mappings.
    /// </summary>
    public IList<Mapping> Mappings { get; set; }

    public MappingViewModel()
    {
        //this.Mappings = new List<Mapping>();
    }

    public string FileName { get; set; }

    #endregion
}

/// <summary>
/// The mapping.
/// </summary>
public class Mapping
{
    #region Public Properties

    public int ColumnIndex { get; set; }

    public string ColumnHeader { get; set; }

    public string DataPreview { get; set; }

    public string PropertyIdentifier { get; set; }

    #endregion
}

我创建了一个强类型的 Razor 视图,在该视图中循环遍历模型内的列表,如下所示:

@model MappingViewModel


@using (Html.BeginForm("ApplyMapping", "Person"))
{
    @Html.EditorFor(t=>t.FileName)
    <table class="i-table fullwidth">
        <thead>
...table header
        </thead>
        @foreach (var mapping in Model.Mappings)
        {
            <tr>
                <td>@Html.EditorFor(x=>mapping.ColumnIndex)</td>
                <td>@Html.EditorFor(x => mapping.ColumnHeader)</td>
                <td>@Html.EditorFor(x => mapping.DataPreview)</td>
                <td>@Html.DropDownListFor(x=>mapping.PropertyIdentifier,(SelectList)ViewBag.PropertyList)</td>
            </tr>
        }
    </table>
    <input type="submit" value="@Resources.Default.ButtonSaveText" class="icon16-button forms-16" />
}

这对我不起作用。数据已显示,但接收控制器操作得到一个空模型 - 文件名除外。

我做错了什么?

附:操作如下:

        [HttpPost]
        public ActionResult UploadAdvanced(FormCollection collection)
        {
 // csv stuff + get property list

            var viewModel = new MappingViewModel();
            viewModel.Mappings = new List<Mapping>();
            for (int i = 0; i < headers.Count(); i++)
            {
                viewModel.Mappings.Add(new Mapping() { ColumnIndex = i, DataPreview = firsLine[i], ColumnHeader = headers[i],PropertyIdentifier = string.Empty });
            }
            viewModel.FileName = file.FileName;
            var propertyList = new SelectList(propList);
            this.ViewBag.PropertyList = propertyList;
            return this.View("Mapping", viewModel);
        }


        [HttpPost]
        public ActionResult ApplyMapping(MappingViewModel model)
        {
            System.Diagnostics.Debug.WriteLine(model.ToString());

            return this.View("Index");
        }

我正在从 ApplyMapping actionResult 中的断点读取视图模型道具。当断点命中-文件名设置正确时,列表为NULL

【问题讨论】:

  • 您好 Jorge,谢谢 - 它们在表单内,我将它们更改为 EditFor,但这并没有改变任何东西。文件名仍然从视图中返回,但列表返回 null

标签: c# asp.net asp.net-mvc viewmodel


【解决方案1】:

它是空的,可能是因为所有属性都使用DisplayFor 而不是EditorFor 第二个它们需要在BeginForm 内,如果您能向我们展示一下操作吗?

更新

将数据类型从IList更改为IEnumerable,然后在构造函数中初始化属性。

/// <summary>
/// The mapping view model.
/// </summary>
public class MappingViewModel
{
    #region Public Properties

    /// <summary>
    /// Gets or sets the mappings.
    /// </summary>
    public IEnumerable<Mapping> Mappings { get; set; }

    public MappingViewModel()
    {
        //this.Mappings = new List<Mapping>();
        IEnumerable<Mapping> test = new HashSet<Mapping>();
    }

    public string FileName { get; set; }

    #endregion
}

更新 2

在此路径 (/Views/Shared/EditorTemplates/) 中为您的类映射创建了一个 EditorTemplate,例如您需要的格式

@model SomePath.Models.Mapping 

<tr> 
    <td>@Html.EditorFor(model => model.ColumnIndex)</td>
    <td>@Html.EditorFor(model => model.ColumnHeader)</td>
</tr>

然后删除视图中的 for 属性 Mapping 并像这样使用EditorFor Helper

@Html.EditorFor(x=>x.Mappings) 

那应该是属性序列化的问题

【讨论】:

  • 您好 Jorge,谢谢 - 它们在表单内,我将它们更改为 EditFor,但这并没有改变任何东西。文件名仍然从视图中返回,但列表返回 null
  • 显示操作以查看发生了什么,更新您对视图更改的疑问
  • 谢谢,完成。是否与列表的可序列化有关?
  • @user896697 还有问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多