I have the following code in my 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");
}
}
}
In my views folder under Home/Index.aspx I have a simple search form like so:
<% 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>
<% } %>
The idea is that when the user submits this form the contents of the query text box will be used and then checked against the headline of an article coming from my database and the Index view will instead of showing all the articles will only show those that match the query typed in by the user.
When I submit the form I get the following error:
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]'.
What have I done wrong here? As from what I can tell the code is fine.