Hi mahesh213,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE Employee(EId INT IDENTITY PRIMARY KEY,EName VARCHAR(20))
INSERT INTO Employee VALUES('mahesh')
INSERT INTO Employee VALUES('mahesh2')
CREATE TABLE Item(ItemId INT IDENTITY PRIMARY KEY,EId INT,ItemName VARCHAR(20),Date DATETIME)
INSERT INTO Item VALUES(1,'aa','2/2/2019')
INSERT INTO Item VALUES(1,'bb','2/4/2019')
SELECT * FROM Employee
SELECT * FROM Item
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult Edit(int ItId, int empId, DateTime? Fdate, DateTime? tdate)
{
ModelEntities db = new ModelEntities();
List<object> ItemList = new List<object>();
if (ItId == 0)
{
var items = (from i in db.Items
join e in db.Employees on i.EId equals e.EId
where i.EId == empId
select new { i.ItemId, i.EId, e.EName, i.ItemName, i.Date }).ToList();
for (DateTime? i = Fdate; i <= tdate; i = i.Value.AddDays(1))
{
List<DateTime?> dates = items.Select(x => x.Date).ToList();
if (dates.Contains(i))
{
var employeeItem = items.Where(x => x.Date == i).FirstOrDefault();
ItemList.Add(new
{
ItemId = employeeItem.ItemId,
ItemName = employeeItem.ItemName,
EId = employeeItem.EId,
EName = employeeItem.EName,
Date = i.Value.ToString("dd/MM/yyyy")
});
}
else
{
var employee = db.Employees.Where(x => x.EId == empId).FirstOrDefault();
if (employee != null)
{
ItemList.Add(new { ItemId = 0, ItemName = DBNull.Value, EId = employee.EId, EName = employee.EName, Date = i.Value.ToString("dd/MM/yyyy") });
}
else
{
foreach (Employee emp in db.Employees.ToList())
{
ItemList.Add(new { ItemId = 0, ItemName = DBNull.Value, EId = emp.EId, EName = emp.EName, Date = i.Value.ToString("dd/MM/yyyy") });
}
}
}
}
}
ItemEntryDetail details = new ItemEntryDetail();
details.item = ItemList;
return Json(details, JsonRequestBehavior.AllowGet);
}
public class ItemEntryDetail
{
public List<object> item { get; set; }
}
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', function ($scope, $window) {
GetItems();
GetEmployees();
function GetItems() {
$scope.Items = [{ Id: 0, Name: 'All' }, { Id: 1, Name: 'aa' }, { Id: 2, Name: 'bb'}];
}
function GetEmployees() {
$scope.employees = [{ EId: 0, EName: 'All' }, { EId: 1, EName: 'mahesh' }, { EId: 2, EName: 'mahesh2'}];
}
$scope.Go = function () {
var Id1 = $scope.Item;
var eId = $scope.Employee;
var fd = $scope.FDate.toISOString().split('T')[0];
var td = $scope.TDate.toISOString().split('T')[0];
$.post("/Home/Edit/", { ItId: Id1, empId: eId, Fdate: fd, tdate: td }, function (r) {
$scope.items = r.item;
$scope.$apply();
});
}
});
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<div class="form-horizontal">
<div class="form-group">
<label for="ID" class="control-label col-xs-2">
Item</label>
<div class="col-md-2">
<select style="display: inline" data-ng-model="Item" class="form-control" data-ng-options="p.Id as p.Name for p in Items">
<option value="">-- Select Item --</option>
</select>
</div>
<label for="ID" class="control-label col-xs-2">
Employee</label>
<div class="col-md-2">
<select style="display: inline" data-ng-model="Employee" class="form-control" data-ng-options="p.EId as p.EName for p in employees">
<option value="">-- Select Item --</option>
</select>
</div>
<div class="col-md-4">
<label for="FDate">
From Date</label>
<input type="date" ng-model="FDate" id="FDate" name="FDate" class="form-control"
required />
</div>
<div class="col-md-4">
<label for="lblTDate">
To Date</label>
<input type="date" ng-model="TDate" id="TDate" name="TDate" class="form-control"
required />
</div>
</div>
</div>
<div>
<button class="btn btn-success btn-sm" ng-click="Go()">
<span class="glyphicon glyphicon-ok"></span>Submit
</button>
</div>
<div class="container" id="printarea">
<table class="table table-bordered">
<tr class="success">
<th>Id</th>
<th>Item</th>
<th>EmployeeId</th>
<th>EmployeeName</th>
<th>Date</th>
</tr>
<tr ng-repeat="item in items">
<td>{{item.ItemId}}</td>
<td>{{item.ItemName}}</td>
<td>{{item.EId}}</td>
<td>{{item.EName}}</td>
<td>{{item.Date}}</td>
</tr>
</table>
</div>
</body>
</html>
Screenshot
