0

我的数据库中有一个表,我选择所有行以将其填充到我视图中的下拉列表中。

我无法理解如何在其中填写值。

有人可以帮帮我吗?

我的代码:

模型:

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(""))<=== ???? 我不知道

4

4 回答 4

2

只需在控制器类型中:

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

并在视图中写道:

@Html.DropDownListFor(model => model.CategoryId, ViewBag.CategoryList as IEnumerable<SelectListItem>, new { @class = "anyclass" })
于 2013-07-30T14:02:37.637 回答
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--"))
于 2013-07-30T13:47:09.897 回答
0

像这样创建一个 ViewModel:

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"))
于 2013-07-30T13:42:42.043 回答
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; }
}
于 2013-07-30T13:42:53.010 回答