【发布时间】:2018-05-10 14:16:50
【问题描述】:
我正在关注一个讲授 ASP.NET MVC 的 YouTube 系列。在教程中,老师展示了如何制作简单的搜索功能,但在我的情况下它有所不同。
我有搜索条件:Studies(下拉菜单)、Country(下拉菜单)、Status(下拉菜单)和关键字强>(输入)。
我的问题是如何查询数据库以根据选择的搜索条件显示结果?
更清楚: 如果用户仅选择了 Studies 和 Country,则代码应使用 Studies 和 Country 中的值来搜索相应的数据库列。
表格:学生
[StudentID] INT IDENTITY (1, 1) NOT NULL,
[StudentName] VARCHAR (50) NOT NULL,
[StudentStudiesID] INT NOT NULL,
[StudentCountry] VARCHAR (50) NOT NULL,
[StudentCity] VARCHAR (50) NOT NULL,
[StudentStatus] VARCHAR (50) NOT NULL,
CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED ([StudentID] ASC),
CONSTRAINT [FK_Students_Studies] FOREIGN KEY ([StudentStudiesID]) REFERENCES [dbo].[Studies] ([StudiesID])
SearchController.cs
public class SearchController : Controller
{
public ActionResult Index()
{
DatabaseEntitiesModel db = new DatabaseEntitiesModel();
int Studies;
int.TryParse(Request.QueryString["Studies"], out Studies);
var Country = Request.QueryString["Country"];
var Status = Request.QueryString["Status"];
var Keyword = Request.QueryString["Keyword"];
IQueryable <Student> SearchQuery = db.Students;
List<SearchViewModel> SVM = SearchQuery.Select(x => new SearchViewModel
{
StudentID = x.StudentID,
StudentName = x.StudentName,
StudentCountry = x.StudentCountry,
StudentCity = x.StudentCity,
StudiesName = x.Study.StudiesName,
StudentStatus = x.StudentStatus
}).OrderByDescending(x => x.StudentID).ToList();
return View( SVM );
}
}
【问题讨论】:
-
“没有线索”不是一个明确的问题陈述。它没有告诉我们您要达到什么目标,也没有告诉我们您面临什么困难或遇到什么错误。您显然有一些 线索,否则您不会编写任何代码。我们不知道您的要求是什么,也不知道您坚持其中的哪一部分。请编辑您的问题以更具体。谢谢。
-
@ADyson 我已将问题编辑得更清楚。
标签: asp.net-mvc entity-framework linq