【发布时间】:2015-12-03 22:59:50
【问题描述】:
所以我正在建立一个图书搜索页面,其他一切正常(按标题、作者等搜索)但我不知道如何通过我们数据库中的 int 唯一编号进行搜索——我已经尝试了几件事比如转换成int32,把ID号转成字符串等等,但还是找不到解决办法
参数:
public ActionResult Index(int IDnumber, string LnameString, string FnameString, string bookGenre,string searchString)
{
//LINQ query to select books
var books = from m in db.Book
select m;
//ID Number
if (IDnumber != null)
/* the if statement gives the following warning which i cannot resolve: the result of the expression is always 'true' since a value of type 'int' is never equal to null of type int?*/
{
books = books.Where(x => x.BookID == IDnumber);
}
...
}
查看代码:
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index", "Books")){
<p>
Genre: @Html.DropDownList("bookGenre", "All")
Title: @Html.TextBox("SearchString") <br />
Author First Name: @Html.TextBox("FnameString")
Author Last Name: @Html.TextBox("LnameString") <br />
ID Number: @Html.TextBox("IDNumber")
<input type="submit" value="Filter" />
</p>
}
确切的错误:
参数字典包含参数的空条目 方法的不可为空类型“System.Int32”的“IDnumber” 'System.Web.Mvc.ActionResult 索引(Int32,System.String, System.String, System.String, System.String)' 在 'SafariBooksGroup15.Controllers.BooksController'。一个可选的 参数必须是引用类型、可为空的类型或声明为 一个可选参数。参数名称:参数
【问题讨论】:
-
books = books.Where(x => x.BookID == Convert.ToInt32(IDnumber));应该只是 books = books.Where(x => x.BookID == IDnumber) 不应该吗?
-
哦,你是对的,对不起,我会编辑
-
IDnumber is already of type int无需对此进行任何转换.. 花时间阅读/调试您的代码 -
您可以将
int?用于IDnumber,如果它有价值,可以将其包含在搜索中。 -
@MethodMan Ya 抱歉,那里没有意义,但是修复它仍然不能解决问题
标签: c# asp.net .net asp.net-mvc entity-framework