【问题标题】:ASP.NET MVC C# Search helpASP.NET MVC C# 搜索帮助
【发布时间】:2013-04-06 06:50:28
【问题描述】:

我的 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]'. 

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

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    似乎在您的视图中您有Inherits="System.Web.Mvc.ViewPage&lt;IEnumerable&lt;Article&gt;&gt;" 但您只传递了一个。要么用一个替换 IEnumerable,要么去掉 .First()。

    【讨论】:

    • 太棒了,修复得很好。我也添加了上面的其他几个问题。谢谢。
    【解决方案2】:

    您传递给的视图是'System.Collections.Generic.IEnumerable1[MyApplication.Models.Article]' 的强类型视图。要么更改视图的类型,要么将一个 IEnumerable 文章集合传递给视图。

    【讨论】:

      【解决方案3】:

      您的视图被强类型化为MyApplication.Models.Article 的集合。您不能将单个 MyApplication.Models.Article 传递到视图中。

      【讨论】:

        【解决方案4】:

        我怎样才能显示有多少 搜索后返回文章 以及查询是什么。还有隐藏 并根据是否显示 用户进行了搜索。

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

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

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

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

        1. 结果计数:&lt;%: Model.Articles.Count() %&gt;
        2. 搜索文字:&lt;%: Model.SearchText %&gt;

        【讨论】:

        • 您能否更详细地解释一下,因为我不完全了解有关使用自定义视图模型(您的意思是只是自定义视图吗?)以及使用模型实例.谢谢
        • @Cameron - 看看这篇文章,看看它是否涵盖了它:stephenwalther.com/blog/archive/2009/04/13/…
        猜你喜欢
        • 1970-01-01
        • 2015-07-16
        • 2011-04-11
        • 2011-01-31
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-04
        相关资源
        最近更新 更多