从详细信息asp.Net mvc 5中搜索日期

时间:2016-09-30 07:12:20

标签: c# asp.net-mvc ef-code-first

on clicking search,nothing happen我试图首先使用asp.net mvc 5代码中的date属性过滤细节。 但它不起作用。 我只创建了一个模型类,代码首先从中生成一个控制器并查看它。 现在我想过滤关于日期的索引页面上的细节。 这是我正在使用的代码。

模特课: -

 public class Meeting
 {
    public int MeetingID { get; set; }
    public String Name { get; set; }
    public String designation { get; set; }
    [Display(Name = "9:00 - 10:00")]
    public String NineToTen { get; set; }
    [Display(Name = "10:00 - 11:00")]
    public String TenToEleven { get; set; }
    [Display(Name = "11:00 - 12:00")]
    public String ElevenToTwelve { get; set; }
    [Display(Name = "12:00 - 1:00")]
    public String TwelveToOne { get; set; }
    [Display(Name = "1:00 - 2:00")]
    public String OneToTwo { get; set; }
    [Display(Name = "2:00 - 3:00")]
    public String TwoToThree { get; set; }
    [Display(Name = "3:00 - 4:00")]
    public String ThreeToFour { get; set; }
    [Required(ErrorMessage = "Please Enter the Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = false)]
    [Display(Name = "Meeting Date")]
    public DateTime Date { get; set; }

}

控制器: -

       using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MSS.Models;

namespace MSS.Controllers
{
    public class MeetingController : Controller
    {
        private OurDbContext db = new OurDbContext();

        // GET: /Meeting/
        public ActionResult Index(DateTime? fromDate, DateTime? toDate)
    {
        if (!fromDate.HasValue) fromDate = DateTime.Now.Date;
        if (!toDate.HasValue) toDate = fromDate.GetValueOrDefault(DateTime.Now.Date).Date.AddDays(1);
        if (toDate < fromDate) toDate = fromDate.GetValueOrDefault(DateTime.Now.Date).Date.AddDays(1);
        ViewBag.fromDate = fromDate;
        ViewBag.toDate = toDate;
        var Meeting = db.meeting.Where(c => c.Date >= fromDate && c.Date < toDate).ToList();

        return View(Meeting);
    }
        // GET: /Meeting/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Meeting meeting = db.meeting.Find(id);
            if (meeting == null)
            {
                return HttpNotFound();
            }
            return View(meeting);
        }

        // GET: /Meeting/Create
        public ActionResult Create()
        {
            return View(new Meeting { Name = User.Identity.Name });
        }

        // POST: /Meeting/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="MeetingID,Name,designation,NineToTen,TenToEleven,ElevenToTwelve,TwelveToOne,OneToTwo,TwoToThree,ThreeToFour,Date")] Meeting meeting)
        {
            if (ModelState.IsValid)
            {
                db.meeting.Add(meeting);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(meeting);
        }

        // GET: /Meeting/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Meeting meeting = db.meeting.Find(id);
            if (meeting == null)
            {
                return HttpNotFound();
            }
            return View(meeting);
        }

        // POST: /Meeting/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="MeetingID,Name,designation,NineToTen,TenToEleven,ElevenToTwelve,TwelveToOne,OneToTwo,TwoToThree,ThreeToFour,Date")] Meeting meeting)
        {
            if (ModelState.IsValid)
            {
                db.Entry(meeting).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(meeting);
        }

        // GET: /Meeting/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Meeting meeting = db.meeting.Find(id);
            if (meeting == null)
            {
                return HttpNotFound();
            }
            return View(meeting);
        }

        // POST: /Meeting/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Meeting meeting = db.meeting.Find(id);
            db.meeting.Remove(meeting);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

查看: -

  @model IEnumerable<MSS.Models.Meeting>

  @{
    ViewBag.Title = "Index";
  }

  <h2>Index</h2>

  <p>
   @Html.ActionLink("Create New", "Create")
  </p>
  @{
    var fromDate = (DateTime)ViewBag.fromDate;
    var toDate = (DateTime)ViewBag.toDate;
  }

 @using (Html.BeginForm("Index", "Meeting", FormMethod.Get))
 {
    <div>
        From Date: @Html.TextBox("fromDate", string.Format("{0:dd MMM yyy}", fromDate), new { @class = "datefield", type = "date" })
        To Date: @Html.TextBox("toDate", string.Format("{0:dd MMM yyy}", fromDate), new { @class = "datefield", type = "date" })
        <input type="submit" value="Search" />
    </div>
}
<table class="table">
 <tr>
     <th>
         @Html.DisplayNameFor(model => model.Name)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.designation)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.NineToTen)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.TenToEleven)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.ElevenToTwelve)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.TwelveToOne)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.OneToTwo)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.TwoToThree)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.ThreeToFour)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.Date)
     </th>
     <th></th>
 </tr>

 @foreach (var item in Model) {
 <tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.designation)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.NineToTen)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.TenToEleven)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ElevenToTwelve)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.TwelveToOne)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.OneToTwo)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.TwoToThree)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ThreeToFour)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.MeetingID }) |
        @Html.ActionLink("Details", "Details", new { id=item.MeetingID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.MeetingID })
    </td>
</tr>
}

1 个答案:

答案 0 :(得分:0)

您将返回完整的数据集,而不是返回已过滤的数据。您可能想尝试以下行 -

return View(Meeting);