在C#/ ASP.NET MVC中编写搜索功能查询

时间:2017-04-15 19:28:58

标签: asp.net-mvc

我正在尝试使用搜索文本框在ASP.NET Web应用程序中包含搜索功能。我有一个控制器(HomeController),其中包括一个索引(列出所有数据)和一个搜索(基于用户输入)操作方法,但我在编写搜索操作方法的代码/查询时遇到问题。任何帮助,将不胜感激。感谢

//textbox from the View
 @using (Html.BeginForm("Search", "Home"))
                    {

                        @Html.Editor("SearchBox")

                        <input type="submit" value="Search" />
                    }

//The model

public class EventViewModel
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public DateTime StartDateTime { get; set; }

        public TimeSpan? Duration { get; set; }

        public string Author { get; set; }

        public string Location { get; set; }

        public static Expression<Func<Event, EventViewModel>> ViewModel
        {
            get
            {
                return e => new EventViewModel()
                {
                    Id = e.Id,
                    Title = e.Title,
                    StartDateTime = e.StartDateTime,
                    Duration = e.Duration,
                    Location = e.Location,
                    Author = e.Author.FullName
                };
            }
        }
    }



public class UpcomingPassedEventsViewModel
    {
        public IEnumerable<EventViewModel> UpcomingEvents { get; set; }

        public IEnumerable<EventViewModel> PassedEvents { get; set; }
    }

//controller


public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            var events = this.db.Events
                .OrderBy(e => e.StartDateTime)
                .Where(e => e.IsPublic)
                .Select(EventViewModel.ViewModel);

            var upcomingEvents = events.Where(e => e.StartDateTime > DateTime.Now);
            var passedEvents = events.Where(e => e.StartDateTime <= DateTime.Now);
            return View(new UpcomingPassedEventsViewModel()
            {
                UpcomingEvents = upcomingEvents,
                PassedEvents = passedEvents
            });
        }

public ActionResult Search(string SearchBox)
        {



        }

0 个答案:

没有答案