我有两张桌子。第一个表product id
包含name
,Department_id
和Departments
列。第二个表Department id
包含name
和import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
...
return ReflectionToStringBuilder.toString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
列。
我想创建一个页面以插入Product表中的Products详细信息,我希望在此页面上应该使用Departments名称填充一个组合框。
答案 0 :(得分:0)
虽然我们不清楚你的问题。但不知何故,我想出了你想要的东西..
模型类
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
}
public class Departments
{
public int DepartmentID{get;set;}
public string DepartmentName {get;set;}
}
<强>控制器强>
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
List<Departments> deps = new List<Departments>();
deps.Add(new Departments() {DepartmentID=1, DepartmentName="IT"});
deps.Add(new Departments() { DepartmentID = 2, DepartmentName = "HR" });
ViewBag.lstDeps = deps;
return View(new Product());
}
查看强>
@model HelloWorldMvcApp.Product
@{
Layout = null;
}
@{
IEnumerable<HelloWorldMvcApp.Departments> CDrop = ViewBag.lstDeps;
List<SelectListItem> selectList = new List<SelectListItem>();
foreach (var c in CDrop)
{
SelectListItem i = new SelectListItem();
i.Text = c.DepartmentName.ToString();
i.Value = c.DepartmentID.ToString();
selectList.Add(i);
}
}
<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h1>Hello Stranger</h1>
@Html.DropDownList("DEpartments", new SelectList(selectList, "Value","Text"))
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(m => m.ProductName)
@Html.TextBoxFor(model => model.ProductName, new {@class="form-control"})
</div>
}
<br/><br/>
</div>
</div>
<!-- JS includes -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
<强> Working Demo 强>
答案 1 :(得分:0)
您可以创建一个组合Product和Departments的模型,并将其从控制器传递到视图。
<小时/>
例如:
public class CompositeModel
{
public Product product { get; set;}
public Department department { get; set;}
}
希望这会有所帮助。