从视图返回的空ViewModel

时间:2012-07-02 19:10:33

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

我正在将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" />
}

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

我做错了什么?

P.S。行动如下:

        [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中的断点读取viewmodel道具。当断点命中 - 文件名设置正确时,列表为NULL

1 个答案:

答案 0 :(得分:2)

它可能是空的,因为所有属性都使用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并使用EditorFor Helper这样

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

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