C# 为什么我的SQL表中的实际数据不显示?

C# 为什么我的SQL表中的实际数据不显示?,c#,asp.net-mvc,C#,Asp.net Mvc,我试图将来自远程服务器的SQL表的数据放入下拉列表中。我需要的信息更新为数据库更新 这是我的控制器 public class HomeController : Controller { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SimulationDBEntities"].ToString()); public List<QuoteTechModel>

我试图将来自远程服务器的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 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%">
    <option value=" ">@ViewBag.techList</option>
</select>

@ViewBag.techList

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

这是因为我试图将ViewBag作为一个整体调用,而不是从中调用一个对象。控制器里还有一些我不需要的东西。谢谢大家

控制器

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 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();
    } 

}
公共类HomeController:控制器
{
SqlConnection conn=新的SqlConnection(ConfigurationManager.ConnectionString[“SimulationDBEntities”].ToString());
公开名单()
{ 
返回conn.Query(“使用SimulationDB;从QuoteTechInfo a中选择a.QTName作为名称”).ToList();
}
公共行动结果索引()
{
ViewBag.techList=List();
返回视图();
} 
}
看法


@foreach(ViewBag.techList中的var行)
{
@行名称
}

obj
变量中是否有任何数据?您也没有
Open()
-ing您的sql连接。您必须有一个设置为每x秒关闭一次的javascript函数。它需要将ajax绑定到包含列表框的局部视图的操作结果。ajax调用成功后,使用ajax调用返回的html更新视图的html。将整个对象列表粘贴到一个选项标记中没有多大意义。你不是想循环它并创建很多选项吗?我循环了选项标签并打开了sql连接,现在它显示了多次“System.Collections.Generic.List'[QuoteChList.Models.QuoteChModel]”是的,你可能想使用其中的一个实际属性,像一个ID或描述,而不是整个对象?