剃刀视图中循环中的DropDown列表

时间:2016-11-30 01:02:40

标签: c# asp.net-mvc asp.net-mvc-4 razor

我正在尝试在剃刀视图中的for循环中使用下拉列表。下拉列表在初始页面加载时正确显示,但模型绑定不适用于帖子。帖子中没有下拉列表中选择的项目。 tableColumns属性为空。任何帮助将非常感激。

模型

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

控制器

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

查看

@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>

}

2 个答案:

答案 0 :(得分:0)

您应该在隐藏元素的集合中呈现tableColumns,下拉值应绑定到SelectedColumn的属性DataMappingViewModel

        @Html.DropDownList("SelectedColumn", new SelectList(Model.XmlElements[i].tableColumns, "COLUMN_NAME", "COLUMN_NAME"), "--Select a Value--")
        @for(var j = 0; j < Model.XmlElements[i].tableColumns.Count; j ++)
        {
            @Html.Hidden(string.Format("XmlElements[{0}].tableColumns[{1}].COLUMN_NAME", i, j), Model.XmlElements[i].tableColumns[j].COLUMN_NAME)
        }

答案 1 :(得分:0)

tableColumns是一个复杂的对象,你不能将<select>元素绑定到一个复杂的对象(它只回发一个简单的值 - 所选选项的值,在你的情况下是值COLUMN_NAME属性。)

您的视图模型需要绑定到的属性,理想情况下需要绑定到

的简化集合属性
public class DataMappingViewModel
{          
    public string TableName { get; set; }
    public List<XmlElementViewModel> XmlElements { get; set; }
    public IEnumerable<string> ColumnList { get; set; } // for generating the SelectLists
}
public class XmlElementViewModel
{
    public string ElementName { get; set; }
    public string SelectedColumn { get; set; } // to bind the selected value to
}

然后你将获得GET方法

public ActionResult Index()
{
    DataMappingViewModel model= new DataMappingViewModel()
    {
        TableName = "TABLENAME",
        ColumnList = data.GetTableColumns("TABLENAME").Select(x => x.COLUMN_NAME),
        XmlElements = new List<XmlElement>()
        {
            new XmlElement()
            {
                ElementName = "element1",
                SelectedColumn = "...." // if you want to preselect an option
            }
        }
    }
    return View(model);
}

然后在视图中

model ExcelImportAPI.Models.DataMappingViewModel
....
@using (Html.BeginForm("MapData", "DataMapping", FormMethod.Post))
{
    ....
    for (var i = 0; i < Model.XmlElements.Count; i++)
    {
        @Html.HiddenFor(m => Model.XmlElements[i].ElementName)
        @Model.XmlElements[i].ElementName
        <div>
            @Html.DropDownListFor(m => m.XmlElements[i].SelectedColumn, new SelectList(Model.ColumnList), "--Select a Value--")
        </div>
    }
    ....
}

附注:

  • 您的视图显示了<td>元素,但没有封闭<tr><table> 元素,但无论如何你似乎不需要一张桌子。
  • 您在视图中不需要@using System.Web.Mvc;
  • 通常,您的SelectList应该在控制器中生成,但是 因为你在循环中使用创建下拉列表,所以你需要 在每次迭代中生成一个新的SelectList,或者使用一个 EditorTemplate并使用SelectList传递additionalViewDatathis answer
  • 中所述
  • DropDownListFor()的第3个参数正在生成null 选项。我假设你会希望用户选择一个值,所以你 应该向[Required]添加SelectedColumn属性并包含 @Html.ValidationMessageFor(m => m.XmlElements[i].SelectedColumn) in 观点。