Hi sani.ss501,
Please refer below sample.
Model
public class Student
{
public int id { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public int ClassId { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View(GetStudents());
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
DateTime startdate = Convert.ToDateTime(form["StartDate"]);
DateTime enddate = Convert.ToDateTime(form["EndDate"]);
DateTime startTime = Convert.ToDateTime(form["StartTime"]);
DateTime endTime = Convert.ToDateTime(form["EndTime"]);
List<Student> students = GetStudents();
bool isValid = true;
foreach (Student student in students)
{
if ((student.StartDate >= startdate && student.EndDate <= enddate))
{
if (Convert.ToDateTime(student.StartTime) <= startTime && Convert.ToDateTime(student.EndTime) >= endTime)
{
isValid = false;
break;
}
}
}
ViewBag.Message = isValid ? "Class Record is Accepted" : "Class Record is not Accepted";
return View(students);
}
private List<Student> GetStudents()
{
List<Student> items = new List<Student>();
items.Add(new Student
{
id = 1,
StartDate = Convert.ToDateTime("15-07-2022"),
EndDate = Convert.ToDateTime("16-07-2022"),
StartTime = "10:20",
EndTime = "12:20",
ClassId = 75
});
items.Add(new Student
{
id = 2,
StartDate = Convert.ToDateTime("17-07-2022"),
EndDate = Convert.ToDateTime("18-07-2022"),
StartTime = "10:30",
EndTime = "12:30",
ClassId = 71
});
return items;
}
}
View
@model List<Interference_of_TwoTimePeriod.Models.Student>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
table { border: 1px solid #ccc; border-collapse: collapse; }
table th { background-color: #F7F7F7; color: #333; font-weight: bold; }
table th, table td { padding: 5px; border: 1px solid #ccc; }
</style>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
<tr>
<td>StartDate:</td>
<td><input type="text" name="StartDate" /></td>
</tr>
<tr>
<td>EndDate:</td>
<td><input type="text" name="EndDate" /></td>
</tr>
<tr>
<td>StartTime:</td>
<td><input type="text" name="StartTime" /></td>
</tr>
<tr>
<td>EndTime:</td>
<td><input type="text" name="EndTime" /></td>
</tr>
</table>
<br />
<input type="submit" value="Submit" />
<br />
@ViewBag.Message
}
</body>
</html>
Screenshot