我正在尝试将来自远程服务器的SQL表中的数据放入下拉列表中。我需要在数据库更新时更新信息。
这是我的控制器
public class HomeController : Controller
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SimulationDBEntities"].ToString());
public List<QuoteTechModel> List()
{
var obj = conn.Query<QuoteTechModel>("USE SimulationDB; SELECT a.QTName As QTName FROM QuoteTechInfo a").OrderByDescending(a => a.QTName).ToList();
List<QuoteTechModel> result = new List<QuoteTechModel>();
foreach (var row in obj)
{
QuoteTechModel model = new QuoteTechModel();
model.QTName = row.QTName;
result.Add(model);
}
return (result);
}
public ActionResult Index()
{
ViewBag.techList = List();
return View();
}
}
这是我的模特
public class QuoteTechModel
{
public string QTName { get; set; }
}
这是我观点的一部分
<select class="form-control" name="SelectQuoteTech" id="SelectQuoteTech" style="width:20%">
<option value=" ">@ViewBag.techList</option>
</select>
答案 0 :(得分:1)
这是因为我试图整体调用ViewBag而不是一件事。控制器中还有一些我不需要的东西。谢谢你们!
控制器
public class HomeController : Controller
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SimulationDBEntities"].ToString());
public List<QuoteTechModel> List()
{
return conn.Query<QuoteTechModel>("USE SimulationDB; SELECT a.QTName As Name FROM QuoteTechInfo a").ToList();
}
public ActionResult Index()
{
ViewBag.techList = List();
return View();
}
}
查看
<select class="form-control" name="SelectQuoteTech" id="SelectQuoteTech" style="width:20%">
@foreach (var row in ViewBag.techList)
{
<option value="">@row.Name</option>
}
</select>
答案 1 :(得分:0)
你有没有尝试添加&#34; Go&#34;在USE SimulationDB之后;?您似乎正在尝试选择数据库。因此,在对该数据库进行任何查询之前,必须先执行批处理。