locked
where >= not limiting data in view RRS feed

  • Question

  • User2074931137 posted

    The controller code below...

    where s.CalendarDate1 >= DateTime.Today

    ...Does not seem to be working to limit data in view.  No errors.
    _________________________________________________

    Four Items  Below:

    1.  CalendarDate Index View as it appears in browser
    2.  CalendarDate Index View
    3.  CalendarDateController / Index Action Method
    4.  CalendarDate Model

    ______________________________________

    Programming Environment: 

    Visual Studio 2013
    .NET 4.5
    C#
    Entity Framework 6
    OS: Windows 7

    See below...
    ______________________________________

    1.
    PDF: CalendarDate / Index View as it appears in browser.

    2.

            // GET: /CalendarDate/
            public ViewResult Index(string sortOrder, string searchString)
            {
                ViewBag.DateSort = String.IsNullOrEmpty(sortOrder) ? "date_desc" : "";
                ViewBag.ClientSort = sortOrder == "client" ? "client_desc" : "client";
    
                var calendars = from s in db.CalendarDates
                                where s.CalendarDate1 >= DateTime.Today               <- Does not seem to be working to limit data in view.
                                select s;
    
                if (!String.IsNullOrEmpty(searchString))
                {
                    calendars = calendars.Where(s => s.CalendarDate1.ToString().Contains(searchString)
                                                    || s.CalendarDateDayOfWeek.Contains(searchString));
                }
                switch (sortOrder)
                {
                    default:
                        calendars = calendars.OrderBy(s => s.CalendarDate1);
                        break;
                    case "date_desc":
                        calendars = calendars.OrderByDescending(s => s.CalendarDate1);
                        break;
                }
                return View(db.CalendarDates.ToList());
            }

    3.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    //
    using System.ComponentModel.DataAnnotations;          // Date Format
    using System.ComponentModel.DataAnnotations.Schema;   // Column
    
    namespace fvm.Models
    {
        public class CalendarDate
        {
            public int CalendarDateID { get; set; }
    
            [DataType(DataType.Date)]
            [Column(TypeName = "DateTime2")]
            [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
            [Display(Name = "Date")]
            public DateTime CalendarDate1 { get; set; }
    
            [DataType(DataType.Date)]
            [Column(TypeName = "DateTime2")]
            [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
            [Display(Name = "Date")]
            public DateTime CalendarDate2 { get; set; }
    
            [DataType(DataType.Time)]
            [Column(TypeName = "DateTime2")]
            [DisplayFormat(DataFormatString = "{0:HH:mm}", ApplyFormatInEditMode = true)]
            public DateTime? CalendarDateTime{ get; set; }
    
            public string ScheduleDateString { get; set; }
    
            [Display(Name = "Day of Week")]
            public string ScheduleDateDayOfWeek { get; set; }
    
            [Display(Name = "Day of Week")]
            public string CalendarDateDayOfWeek { get; set; }
    
            [Display(Name = "Shift")]
            public string CalendarDateAMPM { get; set; }
    
            public string CalendarDateString2 { get; set; }
    
            //public virtual ICollection<Assignment> Assignments { get; set; }
            public virtual ICollection<Schedule> Schedules { get; set; }
        }
    }

    4.

    @model IEnumerable<fvm.Models.CalendarDate>
    
    @{
    ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
    @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
    <tr>
    <th>
    Day of Week
    </th>
    <th>
    Date
    </th>
    <th></th>
    </tr>
    
    @foreach (var item in Model) {
    <tr>
    <td>
    @Html.DisplayFor(modelItem => item.CalendarDateDayOfWeek)
    </td>
    
    <td>
    @Html.DisplayFor(modelItem => item.CalendarDate1)
    </td>
    <td>
    @Html.ActionLink("Edit", "Edit", new { id=item.CalendarDateID }) |
    @Html.ActionLink("Details", "Details", new { id=item.CalendarDateID }) |
    @Html.ActionLink("Delete", "Delete", new { id=item.CalendarDateID })
    </td>
    </tr>
    }
    
    </table>

    Wednesday, January 30, 2019 3:12 PM

Answers

  • User550915133 posted

               return View(db.CalendarDates.ToList());
    

    Well... you don't use the "calendars" variable there...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 6:17 PM

All replies

  • User753101303 posted

    Hi,

    Does it happen only with a searchstring ? If yes, I believe it could be because of the || you have for this situation (and so it would end with WHERE A AND B OR C rather than with WHERE A AND (B OR C)

    Wednesday, January 30, 2019 3:40 PM
  • User-474980206 posted

    You should check the sql generated. 

    Wednesday, January 30, 2019 3:53 PM
  • User550915133 posted

               return View(db.CalendarDates.ToList());
    

    Well... you don't use the "calendars" variable there...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 6:17 PM