I am trying to use a drop-down list within a for loop in a razor view. The drop-down list display correctly on the initial page load, however the model binding is not working on the post. The items selected in the drop-down list are not available on the post. The tableColumns property is empty. Any help would be much appreciated.
Model
public class DataMappingViewModel
{
public string TableName { get; set; }
public List<XmlElement> XmlElements { get; set; }
}
public class XmlElement
{
public string ElementName { get; set; }
public List<SP_GET_DBASE_COLUMNS_Result> tableColumns { get; set; }
}
public partial class SP_GET_DBASE_COLUMNS_Result
{
public string TABLE_CATALOG { get; set; }
public string TABLE_SCHEMA { get; set; }
public string TABLE_NAME { get; set; }
public string COLUMN_NAME { get; set; }
public Nullable<int> ORDINAL_POSITION { get; set; }
public string COLUMN_DEFAULT { get; set; }
public string IS_NULLABLE { get; set; }
public string DATA_TYPE { get; set; }
public Nullable<int> CHARACTER_MAXIMUM_LENGTH { get; set; }
public Nullable<int> CHARACTER_OCTET_LENGTH { get; set; }
public Nullable<byte> NUMERIC_PRECISION { get; set; }
public Nullable<short> NUMERIC_PRECISION_RADIX { get; set; }
public Nullable<int> NUMERIC_SCALE { get; set; }
public Nullable<short> DATETIME_PRECISION { get; set; }
public string CHARACTER_SET_CATALOG { get; set; }
public string CHARACTER_SET_SCHEMA { get; set; }
public string CHARACTER_SET_NAME { get; set; }
public string COLLATION_CATALOG { get; set; }
public string COLLATION_SCHEMA { get; set; }
public string COLLATION_NAME { get; set; }
public string DOMAIN_CATALOG { get; set; }
public string DOMAIN_SCHEMA { get; set; }
public string DOMAIN_NAME { get; set; }
}
Controller
public ActionResult Index()
{
List<XmlElement> elements = new List<XmlElement>();
XmlElement element1 = new XmlElement();
element1.ElementName = "element1";
element1.tableColumns = data.GetTableColumns("TABLENAME");
elements.Add(element1);
DataMappingViewModel viewitem = new DataMappingViewModel();
viewitem.XmlElements = elements;
viewitem.TableName = "TABLENAME";
return View(viewitem);
}
[HttpPost]
public ActionResult MapData(DataMappingViewModel model)
{
return View("Index",model);
}
View
@model ExcelImportAPI.Models.DataMappingViewModel
@using System.Web.Mvc;
@{
ViewBag.Title = "title";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("MapData", "DataMapping", FormMethod.Post))
{
<h2>Data Mapping</h2>
for (var i = 0; i < Model.XmlElements.Count; i++)
{
@Html.HiddenFor(m => Model.XmlElements[i].ElementName)
@Model.XmlElements[i].ElementName
<td>
@Html.DropDownList("tableColumns", new SelectList(@Model.XmlElements[i].tableColumns, "COLUMN_NAME", "COLUMN_NAME"), "--Select a Value--")
</td>
}
<div class="modal-footer">
<input type="submit" name="btnSaveAndExit" value="Save and Exit" class="btn btn-default">
<input type="submit" name="btnSaveAndContinue" value="Save and Continue" class="btn btn-primary modal-btn">
</div>
}