我正在使用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道具。當斷點命中 - 文件名設置正確時,列表爲空
嗨豪爾赫,謝謝 - 他們在表單內,我將它們更改爲EditFor,但這不會改變任何東西。儘管如此,文件名從視圖中返回,但列表返回null –