0

WebサイトにASP.NETMVC4を使用していますが、作成しているものの1つはCSVインポートです。CSV列をORMのプロパティにマップしたいと思います。ファイルをアップロードした後、列番号、1行目(csvファイルのヘッダーを含む)、2行目(データプレビュー)を表示したいと思います。

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

そして、それは私にとってはうまくいきません。データは表示されますが、受信側のコントローラーアクションは、ファイル名を除いて空のモデルを取得します。

私は何が間違っているのですか?

PSアクションは次のとおりです。

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

ApplyMappingactionResultのブレークポイントからビューモデルの小道具を読み取っています。ブレークポイントがヒットした場合-ファイル名が正しく設定されている場合、リストはNULLです

4

1 に答える 1

2

おそらく、すべてのプロパティが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ようにヘルパーを使用します

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

それは属性をシリアル化する問題があるはずです

于 2012-07-02T19:27:03.673 に答える