dropdownlist选择返回null @ mvc4

时间:2014-03-23 17:13:17

标签: asp.net-mvc-4

我正在尝试从具有下拉列表,文本框的视图页面插入数据库..当我输入内容并单击保存意味着我从绑定的下拉选择中得不到任何内容。

My code :

@model    IEnumerable<iWiseapp.Models.LObmodel>

@using (@Html.BeginForm("stop", "Home", FormMethod.Post))
{


     @Html.DropDownList("Data",ViewBag.Data as SelectList,"select a sdsd",new {id="LOB_ID"})

                @Html.DropDownListFor("sss",new SelectList(Model,"lob_id","lob_name"))
                ,
                @Html.DropDownList("LObmodel", new SelectList(ViewBag.data ,"Value","Text"))

               @Html.DropDownListFor(model => model.lob_name, new SelectList(ViewBag.Titles,"Value","Text"))

我尝试了所有可能性,但是我很困惑没有任何工作

添加我的控制器代码

[HttpGet]
        public ActionResult stop()
        {
            ServiceReference1.Service1Client ser_obj = new ServiceReference1.Service1Client();

            IEnumerable<LobList> obj = ser_obj.GetData(); //i am Getting list of data through WCF from BUSINESS LAYER WHERE i created entities via EF

        List<SelectListItem> ls = new List<SelectListItem>();

           foreach (var temp in obj)
        {  

            ls.Add(new SelectListItem() { Text = temp.LOB_NAME, Value = temp.LOB_ID.ToString()           });
        }
        //then create a view model on your controller and pass this value to it
        ViewModel vm = new ViewModel();
        vm.DropDown = ls;  // where vm.DropDown = List<SelectListItem>();


  THE COMMENTED CODE BELOW IS WHAT I AM DOING      


            //var mode_obj = new List<LObmodel>(); 
 //Created LOBmodel class in model which is excat same of entities in Business class 

            //var jobList = new List<SelectListItem>();
            //foreach (var job in obj)
            //{
            //    var item = new SelectListItem();
            //    item.Value = job.LOB_ID.ToString(); //the property you want to display i.e. Title
            //    item.Text = job.LOB_NAME;
            //    jobList.Add(item);
            //}
            //ViewBag.Data = jobList;
           return View(jobList);  or return view (obj)
        }

感谢任何专家建议

我的领域,这些是完美的

public class template
    {
        public List<LobList> LOBs { get; set; } //LOBLIST FROM Entities in business layer

        public int selectedLobId { get; set; }

        public LobList SelectedLob
        {
            get { return LOBs.Single(u=>u.LOB_ID == selectedLobId) ;}
        }
    }

AND 

public class LObmodel
    {
        public int LOB_ID { get; set; }
        public string LOB_NAME { get; set; }
    }

2 个答案:

答案 0 :(得分:0)

我建议将选择列表放入模型中,而不是将其传递到视图包中。您想要的选项是

@Html.DropDownListFor(model => model.lob_name, new SelectList(ViewBag.Titles,"Value","Text"))

您可以通过在控制器上设置model.lob_name来设置所选项目,并在回发后将该值设置为下拉列表的选定值

在控制器上

可以像这样构建列表

List<SelectListItem> ls = new List<SelectListItem>();
foreach(var temp in model){  //where model is your database
    ls.Add(new SelectListItem() { Text = temp.Text, Value = temp.Value });
}
//then create a view model on your controller and pass this value to it
LObmodel vm = new LObmodel();
vm.DropDown = ls;  // where vm.DropDown = List<SelectListItem>();
return View(vm);

然后在您的视图中,您可以参考

@Html.DropDownListFor(x => x.lob_name, Model.DropDown)

使用您的模型添加选择列表

public class LObmodel
{
    public int LOB_ID { get; set; }
    public string LOB_NAME { get; set; }
    public List<SelectListItem> DropDown { get; set; }
}

然后您的视图顶部将是

@model LObmodel

答案 1 :(得分:0)

我遇到了同样的问题。 但我使用以下代码更改了DDL的视图代码:

 @Html.DropDownListFor(x => x.ClassID, (SelectList)ViewBag.ClassName);

下拉列表将绑定到名为ClassID的模型类您将无法将ddl的文本值发布到控制器,只能将ddl后面的ID发布。