我的 HomeController.cs 中有以下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MyApplication.Models;
namespace MyApplication.Controllers
{
public class HomeController : Controller
{
private NewsDBEntities _db = new NewsDBEntities();
//
// GET: /Home/
public ActionResult Index()
{
return View(_db.ArticleSet.ToList());
}
//
// GET: /Home/Details/5
public ActionResult Details(int id)
{
//return View();
var ArticleToView = (from m in _db.ArticleSet
where m.storyId == id
select m).First();
return View(ArticleToView);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(String query)
{
var ArticleQuery = (from m in _db.ArticleSet
where m.headline.Contains(query)
select m).First();
return View(ArticleQuery);
//return RedirectToAction("Search");
}
}
}
在 Home/Index.aspx 下的 View 文件夹中,我有一个简单的搜索表单,如下所示:
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Search</legend>
<p>
<label for="query">Keywords:</label>
<%= Html.TextBox("query") %>
</p>
<p>
<input type="submit" value="Search" />
</p>
</fieldset>
<% } %>
这个想法是,当用户提交此表单时,将使用查询文本框的内容,然后根据来 self 的数据库的文章标题进行检查,索引 View 将只显示而不是显示所有文章那些与用户输入的查询相匹配的内容。
当我提交表单时,出现以下错误:
The model item passed into the dictionary is of type 'MyApplication.Models.Article' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MyApplication.Models.Article]'.
我在这里做错了什么?据我所知,代码很好。
最佳答案
看来在您的 View 中您有 Inherits="System.Web.Mvc.ViewPage<IEnumerable<Article>>"
但你只传递了一个。要么替换 IEnumerable
与一个,或摆脱 .First()
.
关于c# - ASP.NET MVC C# 搜索帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4088587/