Linq查询没有返回值
本文关键字:返回值 查询 Linq | 更新日期: 2023-09-27 17:49:18
我目前有2 dbContexts,我不确定这是否有所不同。ApplicationDbContext和CompanyDBContext。这些表已经在数据库中设置好了,我已经在companydetails表中添加了数据。
我可能对我的代码有几个问题,我不确定我在关于companyID的视图中做了什么是正确的,但主要是我的问题是我不确定为什么我的linq查询在我的表中有数据时不返回任何结果。
My CompanyDBContext goes as is
public class CompanyDBContext : DbContext
{
public DbSet<CompanyDetails> companies { get; set; }
}
这是我的模型
public class CompanyDetails
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int? companyID { get; set; }
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
}
我已经写了一个linq查询,它应该已经得到了我放入数据库的一行。使用这个linq查询,我构造了一个SelectListItems列表,并将其传递给viewbag,然后我希望能够有一个包含公司名称的下拉框,用户可以在其中进行选择。
public ActionResult Register()
{
//var regViewModel = new RegisterViewModel();
List<SelectListItem> companyList = null;
using(var companies = new CompanyDBContext())
{
var companyNamesQuery = (from comp in companies.companies
select new SelectListItem
{
Text = comp.CompanyName,
Value = comp.companyID.ToString()
});
companyList = companyNamesQuery.ToList();
@ViewBag.companies = companyList;
}
return View();
}
这是视图,
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.userCompanyID, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.userCompanyID, new SelectList(ViewBag.companies, "companyID", "CompanyName"))
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
这是我的连接字符串
<add name="DefaultConnection" connectionString="Data Source=xxxxxx'SQLEXPRESS;Initial Catalog=DBTest;User ID=x;Password=Xxxxx1;Persist Security Info=False;" providerName="System.Data.SqlClient" />
<add name="DevConnection" connectionString="Data Source=xxxxxx'SQLEXPRESS;Initial Catalog=DBTest;User ID=x;Password=Xxxxx1;Persist Security Info=False;" providerName="System.Data.SqlClient" />
根据注释,再次检查您的连接字符串。要么是指向错误的服务器、错误的数据库,要么是数据从未进入表。
这条线:
@Html.DropDownListFor(m => m.userCompanyID, new SelectList(ViewBag.companies, "companyID", "CompanyName"))
这一行:
var companyNamesQuery = (from comp in companies.companies
select new SelectListItem
{
Text = comp.CompanyName,
Value = comp.companyID.ToString()
});
您创建了一个新的SelectListItem列表。在视图中,只有Text和Value属性可用。
这个下拉列表应该是:
@Html.DropDownListFor(m => m.userCompanyID, new SelectList(ViewBag.companies, "Value", "Text"))
答案是因为我没有使用正确的连接字符串。当我创建了与dbContext没有相同名称的新连接字符串时,我没有将连接字符串传递给基类