2012-07-02 67 views
0

我正在使用ASP.NET MVC 4的網站,我正在做的事情之一是CSV導入。我想將CSV列映射到ORM的屬性。文件上傳後,我想要顯示列號,第一行(包含csv文件中的標題)和第二行(數據預覽)。空ViewModel從視圖返回

/// <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道具。當斷點命中 - 文件名設置正確時,列表爲空

+0

嗨豪爾赫,謝謝 - 他們在表單內,我將它們更改爲EditFor,但這不會改變任何東西。儘管如此,文件名從視圖中返回,但列表返回null –

回答

2

它是空的可能是因爲所有屬性都使用DisplayFor而不是EditorFor秒他們需要在BeginForm中,如果您能向我們顯示操作?

UPDATE

變化自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

在這個路徑中創建一個EditorTemplate(/查看/共享/ EditorTemplates /)爲您的類映射與您需要例如

@model SomePath.Models.Mapping 

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

然後格式在視圖中刪除for for映射並使用EditorFor這樣的助手

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

這應該是序列化屬性的問題

+0

嗨Jorge,謝謝 - 他們在表單內,我將它們改爲EditFor,但這不會改變任何東西。仍然文件名從視圖中返回,但列表返回null –

+0

向我顯示操作以查看發生了什麼,更新您對視圖中的更改有疑問 – Jorge

+0

謝謝,已完成。它可能與列表的可串行化有關嗎? –