我正试图从webforms转移到MVC,说实话,它并不是很顺利。
我需要一个DropDownList
填充我的数据库中的值,我很难理解如何执行此操作。
我在想,我需要使用实体框架创建一个模型,并将我的DropDownList
引用到那个?但是我该如何指出它呢?
另外,如果我从数据库表中创建模型,如何调整select命令,所以我只得到某些值,而不是整个表?
我知道这是一个广泛的问题,我没有列出任何例子,但我很难找到我能理解的信息,关于我的问题。
答案 0 :(得分:1)
我会从这开始,如果您还没有这样做,那么应该为您创建项目,以便您可以准备好模型
http://msdn.microsoft.com/en-us/data/gg685489
为了创建一个下拉列表,这是一个例子
ViewBag.dropdownlist = new SelectList(db.tablename, "Valuefiled", "NameGField");
其中Valuefiled =数据库中要用于值的列的名称
“NameGField”=数据库中要用于名称
的列的名称获取下拉列表以查看
@Html.DropDownList("dropdownlist")
答案 1 :(得分:1)
如何做到这一点
您的ViewModel
public class CategoryViewModel
{
public Category Category { get; set; }
public IEnumerable<SelectListItem> CategoryTitles { get; set; }
}
您的控制器
public ActionResult Create()
{
var categoryviewmodel = new CategoryViewModel();
categoryviewmodel.Category = new Category();
var list = categoryRepository.AllCategoryTitles().ToList().Select(t => new SelectListItem
{
Text = t.CategoryName,
Value = t.CategoryID.ToString()
})
.ToList();
list.Insert(0, new SelectListItem { Value = "0", Text = "Please Selext" });
categoryviewmodel.CategoryTitles = list;
return View(categoryviewmodel);
}
您的存储库
public IQueryable<Category> AllCategoryTitles()
{
var query = context.Categories.Where(m => m.ParentCategoryID == null && m.IsActive==true);
return query;
}
您的观点
@Html.DropDownListFor(model => model.CategoryParentID, Model.CategoryTitles)
答案 2 :(得分:0)
您可以使用viewModel。这是一个带有一些假设的示例解决方案。请参阅下拉列表(我在下拉列表中列出了“InBound”类型的部门
员工模型
public class EmployeeModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int DeptId { get; set; }
}
部门模型
public class DepartmentModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
ViewModel(将被传递到视图中)
public class EmployeeViewModel
{
public EmployeeModel Employee { get; set; }
public IEnumerable<DepartmentModel> Departments { get; set; }
}
控制器
public ActionResult Index()
{
EmployeeViewModel vm = new EmployeeViewModel();
//This is hardcoded. However you will write your own method to pull the department details with filtering
List<DepartmentModel> departments = new List<DepartmentModel>() { new DepartmentModel { Id = 1, Name = "Accounts", Type = "InBound" }, new DepartmentModel { Id = 2, Name = "Finance", Type = "OutBound" }, new DepartmentModel { Id = 3, Name = "HR", Type = "InBound" } };
vm.Departments = departments.Where(d => d.Type == "InBound");
return View(vm);
}
查看
@model Test1.ViewModels.EmployeeViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.Employee.Id);
<table>
<tr>
<td>@Html.LabelFor(model => model.Employee.FirstName)</td>
<td>@Html.EditorFor(model => model.Employee.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Employee.LastName)</td>
<td>@Html.EditorFor(model => model.Employee.LastName)</td>
</tr>
<tr>
<td>@Html.Label("Department")</td>
<td>@Html.DropDownListFor(model => model.Employee.DeptId, new SelectList(Model.Departments, "Id", "Name"))</td>
</tr>
</table>
}