Jochen van Wylick
Jochen van Wylick

Reputation: 5401

Empty ViewModel returned from view

I'm using ASP.NET MVC 4 for a website, and one of the things I'm making is a CSV import. I want to map the CSV columns onto properties of the ORM. After the file is uploaded, I want to display the column number, the first line (containing the header in the csv file) and the second line (data preview).

/// <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
}

I've created a strongly typed Razor view where I loop through the list inside the model as follows:

@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" />
}

And that's not working for me. The data is displayed, but the receiving controller action gets an empty model - with the exception of the filename.

What am I doing wrong?

P.S. Action is as follows:

        [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");
        }

I'm reading the viewmodel props from a breakpoint in the ApplyMapping actionResult. When the breakpoint hits - filename is set correctly, the list is NULL

Upvotes: 0

Views: 2888

Answers (1)

Jorge
Jorge

Reputation: 18257

It's empty probably because all the properties are using a DisplayFor instead of EditorFor second they need to be inside of the BeginForm, if you Could you shows us the action?

UPDATE

Change the datatype from IList to IEnumerable, second initialize the attribute in the constructor.

/// <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
}

UPDATE 2

Created a EditorTemplate in this path (/Views/Shared/EditorTemplates/) for your class Mapping with the format that you need for example

@model SomePath.Models.Mapping 

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

Then Remove the for in the View for the property Mapping and usee the EditorFor Helper like this

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

That should the problem of serialize the attribute

Upvotes: 2

Related Questions