我目前有一个ASP.NET Core 2.0 Web应用程序(通过默认Web应用程序模板在VS 2017中构建)。我有一个带有内置下拉菜单的RazorPage,我想通过SQL查询来填充它。我已经使用Entity Framework Core来建立数据库模型,如下所示:
public partial class INVOPEContext : DbContext
{
public virtual DbSet<PmeFundData> PmeFundData { get; set; }
modelBuilder.HasAnnotation"Relational:DefaultSchema", "Server\\User");
modelBuilder.Entity<PmeFundData>(entity =>
{
entity.ToTable("PME_FUND_DATA", "dbo");
entity.Property(e => e.Firm).HasMaxLength(255);
});
}
public partial class PmeFundData
{
public string Firm { get; set; }
}
我已经更新了RazorPage PageModel(pmepe.cshtml.cs)以包括DBContext和Query:
public class pmepeModel : PageModel
{
private readonly INVOPEContext _db;
public pmepeModel(INVOPEContext db)
{
_db = db;
}
public IActionResult dropdowns()
{
List<PmeFundData> firmList = new List<PmeFundData>();
firmList = (from Firm in _db.PmeFundData
select Firm).Distinct().ToList();
firmList.Insert(0, new PmeFundData {Firm = "All Firms" });
ViewBag.ListofFirms = firmList;
return View();
}
}
最后,带有下拉菜单(pmepe.cshtml)的视图如下:
@page
@model pmepeModel
@{
ViewData["Title"] = "pmepe";
}
<select asp-for="dropdowns"
id="firm"
class="dropdown"
asp-items= "@(new SelectList(ViewBag.ListofFirms, "Firm"))">
</select>
我收到的错误是PageModel的当前上下文中不存在ViewBag或View(视图中没有错误-Intellisense对其进行了选择)。我在网上找到的每个示例都是针对MVC而不是RazorPages。通常为MVC提供的解决方案是将查询嵌入到Controller中,并在web.config文件中调整MVC版本。但是RazorPages模板没有控制器,我找不到web.config文件-因此我无法使其在我的应用程序中正常工作。您能提供的任何帮助将不胜感激。
答案 0 :(得分:1)
您的代码中存在多个问题。
PmeFundData
应该具有Id
属性,否则,在运行update-database
命令时会收到错误消息。
public partial class PmeFundData
{
public int Id { get; set; }
public string Firm { get; set; }
}
ViewBag
,您可以从Add ViewBag to PageModel #6754跟踪此问题,可以尝试ViewData
或PageModel Property
来绑定对象。
这是ViewData
的简单代码。
public class PmepeModelModel : PageModel
{
private readonly CoreRazor2_1.Data.ApplicationDbContext _context;
public PmepeModelModel(CoreRazor2_1.Data.ApplicationDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public int SelectedFirm { get; set; }
[ViewData]
public IList<PmeFundData> ListofFirms { get {
return Dropdowns();
}
}
public IList<PmeFundData> Dropdowns()
{
List<PmeFundData> firmList = new List<PmeFundData>();
firmList = new List<PmeFundData> {
new PmeFundData{ Id = 1, Firm = "F1"},
new PmeFundData{ Id = 2, Firm = "F3"},
new PmeFundData{ Id = 3, Firm = "F2"}
};
//firmList = (from Firm in _context.PmeFundData
// select Firm).Distinct().ToList();
firmList.Insert(0, new PmeFundData { Firm = "All Firms" });
return firmList;
//ViewData["ListofFirms"] = firmList;
}
public async Task<IActionResult> OnPostAsync()
{
var value = SelectedFirm;
if (!ModelState.IsValid)
{
return Page();
}
_context.PmeFundData.Add(PmeFundData);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
查看
@page
@model CoreRazor2_1.Pages.PmepeModelModel
@{
ViewData["Title"] = "PmepeModel";
}
<h2>PmepeModel</h2>
<h4>PmeFundData</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<select asp-for="@Model.SelectedFirm"
class="dropdown"
asp-items="@(new SelectList((IEnumerable<PmeFundData>)@ViewData["ListofFirms"], "Id" ,"Firm"))">
</select>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
您还可以向Introduction to Razor Pages in ASP.NET Core学习Razor Page