代码之家  ›  专栏  ›  技术社区  ›  Cameron

ASP.NET MVC C搜索帮助

  •  2
  • Cameron  · 技术社区  · 14 年前

    我在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下的视图文件夹中,我有一个简单的搜索表单,如下所示:

        <% 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 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]'. 
    

    我在这里做错了什么?据我所知,代码是好的。

    4 回复  |  直到 12 年前
        1
  •  1
  •   Dave Swersky    14 年前

    要传递给的视图是的强类型视图 'System.Collections.Generic.IEnumerable1[MyApplication.Models.Article]' . 或者更改视图的类型,或者向视图传递一个IEnumerable项目集合。

        2
  •  2
  •   BFree    14 年前

    在你看来 Inherits="System.Web.Mvc.ViewPage<IEnumerable<Article>>" 但你只通过了一个。或者更换 IEnumerable 用一个,或者去掉 .First() .

        3
  •  1
  •   dotariel    14 年前

    您的视图被强类型化为 MyApplication.Models.Article . 你不能通过单程票 myapplication.models.文章 进入视野。

        4
  •  1
  •   dotariel    14 年前

    我怎么数数有多少 搜索后返回文章 以及查询内容。同时隐藏 并根据是否 用户进行了搜索。

    我建议使用自定义视图模型为视图提供所有必要的信息以呈现给用户。

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

    在控制器中,您将创建这个模型的新实例,并相应地填充它。

    然后您将强烈地键入您的视图 SearchResultViewModel ,并在需要时使用:

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