Asp.net code to filter news by date

Here is an example of how you can filter news by date in ASP.NET:

Assumptions:

Code:

NewsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using YourProject.Models;

namespace YourProject.Controllers
{
    public class NewsController : Controller
    {
        // Get all news items
        public ActionResult Index()
        {
            var news = NewsModel.GetAllNews();
            return View(news);
        }

        // Get news items by date range
        public ActionResult ByDate(DateTime startDate, DateTime endDate)
        {
            var news = NewsModel.GetNewsByDateRange(startDate, endDate);
            return View(news);
        }
    }
}

NewsModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using YourProject.Models;

namespace YourProject.Models
{
    public class NewsModel
    {
        public static List<News> GetAllNews()
        {
            // Get all news items from database
            using (var db = new YourProjectDbContext())
            {
                return db.News.ToList();
            }
        }

        public static List<News> GetNewsByDateRange(DateTime startDate, DateTime endDate)
        {
            // Get news items by date range from database
            using (var db = new YourProjectDbContext())
            {
                return db.News.Where(n => n.DatePublished >= startDate && n.DatePublished <= endDate).ToList();
            }
        }
    }
}

News.cs

using System;

namespace YourProject.Models
{
    public class News
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public DateTime DatePublished { get; set; }
    }
}

View (e.g. News.cshtml)

@model List<YourProject.Models.News>

@{
    ViewBag.Title = "News";
}

<h2>News</h2>

<form action="@Url.Action("ByDate", "News")" method="get">
    <label for="startDate">Start Date:</label>
    <input type="date" id="startDate" name="startDate" />
    <label for="endDate">End Date:</label>
    <input type="date" id="endDate" name="endDate" />
    <input type="submit" value="Filter" />
</form>

<table>
    <thead>
        <tr>
            <th>Title</th>
            <th>Date Published</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var news in Model)
        {
            <tr>
                <td>@news.Title</td>
                <td>@news.DatePublished.ToString("yyyy-MM-dd")</td>
            </tr>
        }
    </tbody>
</table>

In this example, the NewsController has two actions: Index and ByDate. The Index action returns all news items, while the ByDate action returns news items by date range.

The NewsModel class has two methods: GetAllNews and GetNewsByDateRange. The GetAllNews method returns all news items from the database, while the GetNewsByDateRange method returns news items by date range.

The view uses a form to allow the user to select a start and end date, and then submits the form to the ByDate action. The ByDate action uses the selected dates to filter the news items and returns the filtered list to the view.

Note that this is just a basic example, and you may want to add additional functionality (e.g. error handling, pagination) to your news filtering system.