akhter says:
return
View(Itemlist);
ItemList is Collection.
But the Model for the View is defined as single object. So you need to pass the ItemMV as model object.
Refer the modified code.
public IActionResult ItemList()
{
var Itemlist = (from i in _context.ItemMasterFiles
join s in _context.Sections on i.SecId equals s.SecId
join c in _context.Catagories on i.Cid equals c.Cid
into ce
from sub in ce.DefaultIfEmpty()
where i.Packsize == "1" || i.Delid == null
select new Item()
{
CodeItem = i.CodeItem,
Descriptionitem = i.Descriptionitem,
Secnam = s.Secnam,
BaleSize = i.BaleSize,
Weight = i.Weight,
Cname = sub.Cname,
}).ToList();
ItemMV seclist = new ItemMV();
var sectionList = (from s in _context.Sections
select new SelectListItem()
{
Text = s.Secnam,
Value = s.SecId.ToString()
}).ToList();
sectionList.Insert(0, new SelectListItem()
{
Text = "----Select----",
Value = string.Empty
});
seclist.Listofsections = sectionList;
seclist.Items = Itemlist;
return View(seclist);
}
Example
Model
public class ItemMV
{
public List<Item> Items { get; set; }
public int SecId { get; set; }
[NotMapped]
public IEnumerable<SelectListItem> Listofsections { get; set; }
}
public class Item
{
public int CodeItem { get; set; }
public string Descriptionitem { get; set; }
public string BaleSize { get; set; }
public int Weight { get; set; }
public int SecId { get; set; }
[DisplayName("Section")]
public string Secnam { get; set; }
[DisplayName("Category")]
public string Cname { get; set; }
public string Packsize { get; set; }
public string Alid { get; set; }
public int Iduom { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public IActionResult ItemList()
{
ItemMV seclist = new ItemMV();
List<Item> Itemlist = (from i in _context.ItemMasterFiles
join s in _context.Sections on i.SecId equals s.SecId
join c in _context.Catagories on i.Cid equals c.Cid
into ce
from sub in ce.DefaultIfEmpty()
where i.Packsize == "1" || i.Delid == null
select new Item()
{
CodeItem = i.CodeItem,
Descriptionitem = i.Descriptionitem,
Secnam = s.Secnam,
BaleSize = i.BaleSize,
Weight = i.Weight,
Cname = sub.Cname
}).ToList();
var sectionList = (from s in _context.Sections
select new SelectListItem()
{
Text = s.Secnam,
Value = s.SecId.ToString()
}).ToList();
sectionList.Insert(0, new SelectListItem() { Text = "----Select----", Value = string.Empty });
seclist.Listofsections = sectionList;
seclist.Items = Itemlist;
return View(seclist);
}
}
View
@model Sample_950818.Models.ItemMV
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div class="card-body">
<a asp-action="Create" class="btn btn-primary" asp-controller="Items">
Create New
</a>
<hr />
<form asp-action="Create">
<div class="form-group">
<div class="alert-danger" asp-validation-summary="ModelOnly"></div>
<label asp-for="SecId">Section</label>
<select asp-for="SecId"
class="form-select-sm form-control" data-live-search="true"
asp-items="@(new SelectList(Model.Listofsections, "Value", "Text") )">
</select>
</div>
</form>
<table class="table table-striped my-4 w-100" id="datatable2">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Items[0].CodeItem)
</th>
<th>
@Html.DisplayNameFor(model => model.Items[0].Descriptionitem)
</th>
<th>
@Html.DisplayNameFor(model => model.Items[0].Secnam)
</th>
<th>
@Html.DisplayNameFor(model => model.Items[0].Cname)
</th>
<th>
@Html.DisplayNameFor(model => model.Items[0].BaleSize)
</th>
<th>
@Html.DisplayNameFor(model => model.Items[0].Weight)
</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@if (Model.Items != null && Model.Items.Any())
{
@foreach (var item in Model.Items)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CodeItem)
</td>
<td>
@Html.DisplayFor(modelItem => item.Descriptionitem)
</td>
<td>
@Html.DisplayFor(modelItem => item.Secnam)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cname)
</td>
<td>
@Html.DisplayFor(modelItem => item.BaleSize)
</td>
<td>
@Html.DisplayFor(modelItem => item.Weight)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.CodeItem" class="btn btn-warning">Edit</a> |
<a asp-action="Delete" asp-route-id="@item.CodeItem" class="btn btn-danger">Delete</a>
</td>
</tr>
}
}
else
{
<tr>
<td colspan="6">
<div>
No Employee Data is available..
</div>
</td>
</tr>
}
</tbody>
</table>
</div>
</body>
</html>