2013-07-30 102 views
0

我在我的数据库中有一个表格,我选择所有行填充到我的视图中的下拉列表中。用数据库中的值填充下拉列表 - MVC4

我无法理解我该如何填写数值。

有人能帮我一把吗?

我的代码:

型号:

public class MyList 
    { 
     public int id { get; set; } 
     public string name{ get; set; } 
    } 

    public class Empresas 
    { 
     public static IEnumerable<MyList> Getmyinformation() 
     { 
      var list = new List<MyList>(); 
      string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 

      using (var con = new SqlConnection(connection)) 
      { 
       con.Open(); 
       using (var command = new SqlCommand("SELECT * FROM mytable", con)) 
       { 
        SqlDataReader reader = command.ExecuteReader(); 
        while (reader.Read()) 
        { 
         string Name= reader[1] as string; 
         list.Add(new MyList() { name= Name}); 
        } 
       } 
       con.Close(); 
      } 
      return list; 
     } 
    } 

    public class DefaultConnection : DbContext 
    { 
     public DbSet<MyList> lat { get; set; } 
    } 

控制器:

private DefaultConnection db = new DefaultConnection(); 
public ActionResult Add() 
     { 
      return View(db.lat.ToList()); 
     } 

查看:

@Html.DropDownListFor("-- Select --", new SelectList("")) < === ????我不知道

+0

你想用这个下拉菜单做什么?它应该填充任何模型属性? –

+0

我想从我的表中选择数据并自动将其插入到我的下拉列表中。因此,用户可以选择一个 – user2232273

+0

@ user2232273为什么使用ADO.Net和EF? – jonni

回答

0

像这样创建一个视图模型:

public class ListViewModel 
{ 
    public MyList MyList { get; set; } 
    public int SelectedId { get; set; } 
} 

然后,改变你的行动是:

public ActionResult Add() 
{ 
    var viewModel = new ListViewModel { MyList = db.lat.ToList() }; 
    return View(viewModel); 
} 

,然后,这是你将在你查看的内容:

@model MyApp.ViewModels.ListViewModel 

@Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.MyList as IEnumerable, "Id", "Name")) 
+0

你是什么意思创建一个viewmodel?修改我自己的或创建一个新的? – user2232273

+0

创建一个新的。而且,我建议您阅读有关MVC的更多信息。 – ataravati

+0

我知道..即时通讯工作了解更多。 – user2232273

0

试试这个,

查看: -

@Html.DropDownListFor(m => m.CustomerId, Model.customerNameList, "--Select--") 

控制器: -

public ActionResult CustomerInfo() 
     { 

      var List = GetCustomerName(); 
      ViewBag.CustomerNameID = new SelectList(List, "CustomerId", "customerName"); 
      ViewBag.RegisterItems = GetAllRegisterData(); 
      return View(); 
     } 

public List<CustomerModel> GetCustomerName() 
     { 
      // Customer DropDown 
      using (dataDataContext _context = new dataDataContext()) 
      { 
       return (from c in _context.Customers 
         select new CustomerModel 
         { 
          CustomerId = c.CID, 
          customerName = c.CustomerName 
         }).ToList<CustomerModel>(); 
      } 
     } 

型号:

public class CustomerModel 
    { 
     public int CustomerId { get; set; } 

     [StringLength(9), Required, DisplayName("Social security number")] 
     [RegularExpression(@"\d{3}-\d\d-\d{4}", ErrorMessage = "Invalid social security number")] 
     public string customerName { get; set; } 

     public List<MyListItems> customerNameList { get; set; } 
} 
1

在我的实践,我创建下拉列表如下:

首先我创建视图模型

public class MyObj 
{ 
    public int id { get; set; } 
    public string name{ get; set; } 
} 
// viewmodel 
public class MyviewModel 
{ 
    public IQuerable<MyObj> MyObjs{get;set;} 
    public Other Other{get;set;} 
} 

然后我通过该模型从控制器以查看

private DefaultConnection db = new DefaultConnection(); 
public ActionResult Index() 
{ 
    var drop = new MyviewModel 
    { 
     MyObjs = db.MyObjs,// selecting table... 
     Other = new Other 
    } 
    return View(drop); 
} 

在控制器

@Html.DropDownListFor(model => model.Other.MyObjId, new SelectList(Model.MyObjs , "id", "name","--select--")) 
+0

我在哪里从我的表中选择? – user2232273

+0

我编辑了我的答案,请认真看看。 –

+0

是的,我看到埃尔文... thx的帮助..但我失踪的东西,因为我无法从我的数据库中选择任何表 – user2232273

2

只需在控制器类型:

ViewBag.CategoryList = new SelectList(db.Categories.ToList(), "Id", "Name"); 

并鉴于写:

@Html.DropDownListFor(model => model.CategoryId, ViewBag.CategoryList as IEnumerable<SelectListItem>, new { @class = "anyclass" }) 
相关问题