MVC下拉列表使用具有不同所选数据的相同源数据

时间:2016-03-14 17:56:28

标签: asp.net-mvc asp.net-mvc-4

我已经搜索了我能做什么,如果我错过了一些原谅我的话。但是,我找不到我想要的东西。

我想要完成的是: 在我看来,我需要生成总共9行以下内容: 键入(作为下拉列表),操作(作为下拉列表)和项目数(文本框)。

我正在使用下面的模型:

public partial class PM_REFERENCE_DATA_CHANGES
{
    public string ID { get; set; }
    public string REFERENCE_DATA_TYPE { get; set; }
    public string ACTION { get; set; }
    public Nullable<decimal> QUANTITY { get; set; }
}

这在MVC中使用,需要能够传回要保存到数据库的所有9行数据(Type,Action,Number)。

这是我当前的代码,虽然它产生了正确的数字行,但我遇到的问题是在正确的行上获得正确的信息:

            //Component are pulled from PM_REFERENCE_DATA_CHANGES
        //Get components of request
        var components = db.PM_REFERENCE_DATA_CHANGES.Where(r => r.ID == id).OrderBy(r => r.REFERENCE_DATA_TYPE).ToList();

        //Get All Current Data Types
        var getTypes = db.PM_REFERENCE_DATA_TYPES.ToList();

        //Get the nubmer of components for the request
        var numberOfComponents = db.PM_REFERENCE_DATA_CHANGES.Where(r => r.ID == id).Count();

        //Create Action List
        List<SelectListItem> actions = new List<SelectListItem>();
        actions.Add(new SelectListItem { Text = "", Value = "", Selected = true });
        actions.Add(new SelectListItem { Text = "Add", Value = "ADD" });
        actions.Add(new SelectListItem { Text = "Edit", Value = "EDIT" });
        actions.Add(new SelectListItem { Text = "Delete", Value = "DELETE" });


        var componentArray = new List<PM_REFERENCE_DATA_CHANGES>();           

        if (components.Any())
        {
            //Loop through each row of components to build dropdown for Type, Action, Number
            foreach (var component in components)
            {

                //Create Type Dropdown
               // componentArray.Add(component);


                if (getTypes.Any())
                {
                    var list = new List<SelectListItem>();
                    list.Add(new SelectListItem { Text = "", Value = "" });
                    var item = new SelectListItem();
                    foreach (var type in getTypes)
                    {

                        if (component.REFERENCE_DATA_TYPE.ToString().Equals(type.REFERENCE_DATA_TYPE.ToString()))
                        {
                            item = new SelectListItem() { Text = type.REFERENCE_DATA_TYPE.ToString(), Value = type.REFERENCE_DATA_TYPE.ToString(), Selected = true };
                        }
                        else 
                        {
                            item = new SelectListItem() { Text = type.REFERENCE_DATA_TYPE.ToString(), Value = type.REFERENCE_DATA_TYPE.ToString() };
                        }

                        list.Add(item);
                    }

                    ViewBag.Types = list;
                    ViewData["Types"] = list;

                }

                componentArray.Add(component);

            }

            //for (var i = 1; i <= (9 - Convert.ToInt16(ViewBag.numberOfComponents)); i++)
            //{
            //    var component = new PM_REFERENCE_DATA_CHANGES();
            //    componentArray.Add(component);

            //}


        }


        ViewData["actions"] = actions;

        ViewBag.numberOfComponents = numberOfComponents;

        return PartialView(componentArray);

查看:

@foreach (var item in Model)
{
  <div class="row">
        <div class="col-sm-6">
        <strong>:&nbsp;&nbsp;</strong> @Html.DropDownList("Types", ViewData["Types"] as SelectList)
        </div>
        <div class="col-sm-3 text-center">
            @Html.DropDownList("actions", ViewData["actions"] as SelectList,item.ACTION)
        </div>
        <div class="col-sm-3 text-center" style="width: 15px">
            @Html.TextBox("quantity", item.QUANTITY.ToString())
        </div>
    </div>

}

我知道我很接近这一点,但无法弄清楚如何让它做我需要的。有什么建议?

0 个答案:

没有答案