开发者

ASP.NET MVC C# Search help

开发者 https://www.devze.com 2023-01-23 13:19 出处:网络
I have the following code in my HomeController.cs using System; using System.Collections.Generic; using System.Linq;

I have the following code in my HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mv开发者_运维技巧c;
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.


It seems that in your View you have Inherits="System.Web.Mvc.ViewPage<IEnumerable<Article>>" yet you're only passing it a single one. Either replace the IEnumerable with a single one, or get rid of the .First().


The View you're passing to is a strongly-typed view of 'System.Collections.Generic.IEnumerable1[MyApplication.Models.Article]'. Either change the type of the view, or pass an IEnumerable collection of Articles to the View.


Your view is strongly typed to a collection of MyApplication.Models.Article. You cannot pass a single MyApplication.Models.Article into the view.


How can I show a count of how many articles are returned after the search and what the query was. And also hide and show it based on whether or not the user did a search.

I would recommend using a custom View Model to provide the View all the necessary information to render to the user.

public class SearchResultViewModel {
   public IEnumerable<MyApplication.Models.Article> Articles { get; set; }
   public string SearchText { get; set; }
}

In your controller, you would create new instance of this model, and populate it accordingly.

Then you would strongly type your view to SearchResultViewModel, and use it where needed:

  1. Results Count: <%: Model.Articles.Count() %>
  2. Search Text: <%: Model.SearchText %>
0

精彩评论

暂无评论...
验证码 换一张
取 消