简体   繁体   English

ASP.NET MVC C#搜索帮助

[英]ASP.NET MVC C# Search help

I have the following code in my HomeController.cs 我的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: 在Home / Index.aspx下的views文件夹中,有一个简单的搜索表单,如下所示:

    <% 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. 似乎在您的View中,您具有Inherits="System.Web.Mvc.ViewPage<IEnumerable<Article>>"但您只传递了一个。 Either replace the IEnumerable with a single one, or get rid of the .First() . 可以将IEnumerable替换为单个IEnumerable ,或者摆脱.First()

Your view is strongly typed to a collection of MyApplication.Models.Article . 您的视图已强烈键入MyApplication.Models.Article的集合。 You cannot pass a single MyApplication.Models.Article into the view. 您不能将单个MyApplication.Models.Article传递到视图中。

The View you're passing to is a strongly-typed view of 'System.Collections.Generic.IEnumerable1[MyApplication.Models.Article]' . 传递给的视图是'System.Collections.Generic.IEnumerable1[MyApplication.Models.Article]'的强类型视图。 Either change the type of the view, or pass an IEnumerable collection of Articles to the View. 更改视图的类型,或将IEnumerable的Articles集合传递给视图。

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: 然后,将您的视图强烈键入SearchResultViewModel ,并在需要的地方使用它:

  1. Results Count: <%: Model.Articles.Count() %> 结果计数: <%: Model.Articles.Count() %>
  2. Search Text: <%: Model.SearchText %> 搜索文本: <%: Model.SearchText %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM