为什么我的SQL表中的实际数据没有显示?

问题描述 投票:2回答:2

我试图将来自远程服务器的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>
c# asp.net-mvc
2个回答
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>

0
投票

您是否尝试在USE SimulationDB之后添加“Go”;?您似乎正在尝试选择数据库。因此,在对该数据库进行任何查询之前,必须先执行批处理。

© www.soinside.com 2019 - 2024. All rights reserved.