本文介绍了从详细信息中搜索日期asp.Net mvc 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击搜索后,什么都没发生我正在尝试使用date属性过滤详细信息首先在asp.net mvc 5代码中.但它不起作用.我只创建了一个模型类,代码首先从该模型类中生成一个控制器并对其进行查看.现在,我想针对日期过滤索引页面上的详细信息.这是我正在使用的代码.

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.

模型类:-

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

推荐答案

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

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

return View(Meeting);

这篇关于从详细信息中搜索日期asp.Net mvc 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:52