Search date from the detail asp.Net mvc 5

拜拜、爱过 提交于 2021-02-08 12:07:07

问题


on clicking search,nothing happenI am trying to filter the detail with date property in asp.net mvc 5 code first. but it's not working. i have only create a model class from which code first generate a controller and view for it. now i want to filter the detail on the index page with respect to date. here is the code i am using.

Model Class:-

 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; }

}

Controller:-

       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);
        }
    }
}

View:-

  @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:


You are returning the complete dataset instead of returning the filtered data. You might wanna try the below line -

return View(Meeting);


来源:https://stackoverflow.com/questions/39785736/search-date-from-the-detail-asp-net-mvc-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!