Populate View with 2 Lists (Group by LINQ queries) - c#

I am new to ASP.net MVC and I have looked at other questions and tried multiple methods to populate a view with 2 IEnumerables using a ViewModel. It seems using the groupby methods in the controller and passing them to the View has created some problems.
The overall problem is that the view doesn't accept ListCatViewModel. It gives the error: IEnumerable does not contain a definition for 'Category' and no extension method 'Category' accepting a first argument of type 'IEnumerable
In the multiple questions that I have seen on Stackoverflow and other sources we are supposed to combine the two lists with a variable and then pass that variable to the View. After that we are supposed to reference the ViewModel. When I reference the ViewModel I cannot access the properties in each IEnumerable (NotesList and RelList). I am looking for direction on how to change the controller and/or reference in the View to accept the 2 separate collections to ultimately create two tables.
Summary of Code: CatViewModel and RelViewModel hold different properties. ListCatViewModel contains the IEnumerable collections.
public class CatViewModel
{
public string Category { get; set; }
public decimal CountOfLoans { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal TotalVolume { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal UnfundedCommitment { get; set; }
}
public class RelViewModel
{
public string RelationshipName { get; set; }
public decimal CountOfLoans { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal TotalVolume { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal UnfundedCommitment { get; set; }
}
public class ListCatViewModel
{
public IEnumerable<CatViewModel> NotesList { get; set; }
public IEnumerable<RelViewModel> RelList { get; set; }
}
Controller:
public ActionResult Index()
{
ApplicationDbContext db = new ApplicationDbContext();
ListCatViewModel LCVM = new ListCatViewModel();
LCVM.NotesList = GetCatList();
LCVM.RelList = GetRelList();
return View(LCVM);
}
public IEnumerable<CatViewModel> GetCatList() //Category group by query
{
ApplicationDbContext db = new ApplicationDbContext();
IEnumerable<CatViewModel> CatVM = db.LoanPortfolio.GroupBy(i => i.Category)
.Select(g => new CatViewModel
{
Category = g.Key,
CountOfLoans = g.Count(),
TotalVolume = g.Sum(i => i.Volume),
UnfundedCommitment = g.Sum(i => i.Unfunded),
//Average = g.Average(i => i.Volume)
})
.OrderBy(c => c.Category)
.AsEnumerable();
return CatVM;
}
public IEnumerable<RelViewModel> GetRelList()
{
ApplicationDbContext db = new ApplicationDbContext();
IEnumerable<RelViewModel> RelVM = db.LoanPortfolio.GroupBy(i => i.RelationshipName)
.Select(g => new RelViewModel
{
RelationshipName = g.Key,
CountOfLoans = g.Count(),
TotalVolume = g.Sum(i => i.Volume),
UnfundedCommitment = g.Sum(i => i.Unfunded),
//Average = g.Average(i => i.Volume)
})
.OrderBy(c => c.RelationshipName)
.AsEnumerable();
return RelVM;
}
View:
View:
#model IEnumerable<Risk.ViewModel.ListCatViewModel>
#{
ViewBag.Title = "GroupByLoanPAllowanceB";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Category</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.NotesList.Category)
</th>
<th>
#Html.DisplayNameFor(model => model.NotesList.CountOfLoans)
</th>
<th>
#Html.DisplayNameFor(model => model.NotesList.TotalVolume)
</th>
<th>
#Html.DisplayNameFor(model => model.NotesList.UnfundedCommitment)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.NotesList.Category)
</td>
<td>
#Html.DisplayFor(modelItem => item.CountOfLoans)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalVolume)
</td>
<td>
#Html.DisplayFor(modelItem => item.UnfundedCommitment)
</td>
</tr>
}
</table>

Your view is not IEnumerable<Risk.ViewModel.ListCatViewModel>, it is now ListCatViewModel
Try:
#model Risk.ViewModel.ListCatViewModel
#{
ViewBag.Title = "GroupByLoanPAllowanceB";
Layout = "~/Views/Shared/_Layout.cshtml";
}
And when iterating over the list, you need to access it off the Model
#foreach (var item in Model.NotesList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Category)
</td>
<td>
#Html.DisplayFor(modelItem => item.CountOfLoans)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalVolume)
</td>
<td>
#Html.DisplayFor(modelItem => item.UnfundedCommitment)
</td>
</tr>
}

Assuming you're just trying to display the data, create some templates in the Views -> Shared -> DisplayTemplates folder (you may need to create it), and then use DisplayFor. Here's an example for your CatViewModel:
CatViewModel.cshtml
#model Your.Fully.Qualified.Namespace.CatViewModel
<tr>
<td>
#Model.Category
</td>
<td>
#Model.CountOfLoans
</td>
<td>
#Model.TotalVolume
</td>
<td>
#Model.UnfundedCommitment
</td>
</tr>
Parent view:
#model Risk.ViewModel.ListCatViewModel
#{
ViewBag.Title = "GroupByLoanPAllowanceB";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Category</h2>
<table class="table">
<tr>
<th>
Category
</th>
<th>
Count Of Loans
</th>
<th>
Total Volume
</th>
<th>
Unfunded Commitment
</th>
</tr>
#Html.DisplayFor(m => m.NotesList)
</table>
The Razor engine is smart enough to look for a partial view with the same name as the model (in the aforementioned folder) when you call either EditorFor or DisplayFor. So, if the property was of type Foo, and you had a view called Foo.cshtml, it would be used for that property. If you supply a collection, it will also iterate through the collection and generate markup for each item.
There's also an overload that allows you to specify the template to use, if you don't want to follow the convention I mentioned above.

Related

Why do I get this error when passing ViewModel to View?

When passing ViewModel to View I get the error
The model item passed into the ViewDataDictionary is of type
'System.Collections.Generic.List'1[TraficAlert.Models.TaBarHeader]',
but this ViewDataDictionary instance requires a model item of type
'System.Collections.Generic.IEnumerable'1[TraficAlert.Models.ViewModels.HeaderTelegramaViewModel]'.
I have tried to use #model IEnumerable<TraficAlert.Models.ViewModels.HeaderTelegramaViewModel> in the Index.cshtml and it works, but I need to access a property from HeaderTelegramaViewModel.
Index.cshtml:
#model IEnumerable<TraficAlert.Models.ViewModels.HeaderTelegramaViewModel>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.TaBarHeader.Id)
</th>
<th>
#Html.DisplayNameFor(model => model.TaBarHeader.ParentId)
</th>
<th>
#Html.DisplayNameFor(model => model.TaBarHeader.TStamp)
</th>
(...)
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.TaBarHeader.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.TaBarHeader.ParentId)
</td>
<td>
#Html.DisplayFor(modelItem => item.TaBarHeader.TStamp)
</td>
(...)
<td>
<a asp-action="Edit" asp-route-id="#item.TaBarHeader.Id">Edit</a> |
<a asp-action="Details" asp-route-id="#item.TaBarHeader.Id">Details</a> |
<a asp-action="Delete" asp-route-id="#item.TaBarHeader.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
HeaderTelegramaController:
(...)
public IActionResult Index()
{
var applicationDbContext = _unitofwork.BarHeader.GetAllBarH().ToList();
return View(applicationDbContext);
}
TaBarHeaderRepository:
public IEnumerable<TaBarHeader> GetAllBarH()
{
return _db.TaBarHeaders
.Include(t => t.CategoriaFk)
.Include(t => t.CauzaFk)
.Include(t => t.ClasaFk)
.Include(t => t.LucrareFk)
.Include(t => t.RegionalaFk)
.Include(t => t.UserFk);
}
HeaderTelegramaViewModel:
public TaBarHeader TaBarHeader { get; set; }
public IEnumerable<SelectListItem> Categoria { get; set; }
public IEnumerable<ViewOtf> ViewOTFCollection { get; set; }
(...)
Why do I get the above mentioned error?
Thank you.
use the model below in the cshtml.
#model TraficAlert.Models.ViewModels.HeaderTelegramaViewModel
And in the Index() create an instance of HeaderTelegramaViewModel:
var _HeaderTelegramaViewModel = new HeaderTelegramaViewModel();
_HeaderTelegramaViewModel.TaBarHeader = TaBarHeader;
And the class HeaderTelegramaViewModel must have:
public IEnumerable<TaBarHeader> TaBarHeader { get; set; }
public IEnumerable<SelectListItem> Categoria { get; set; }
public IEnumerable<ViewOtf> ViewOTFCollection { get; set; }
use :: #model TraficAlert.Models.ViewModels.HeaderTelegramaViewModel
instead of ::#model IEnumerable<TraficAlert.Models.ViewModels.HeaderTelegramaViewModel>
at the top of index.cshtml page
See the type of your model IEnumerable<TraficAlert.Models.ViewModels.HeaderTelegramaViewModel> and apply this:
public IActionResult Index()
{
var applicationDbContext = _unitofwork.BarHeader.GetAllBarH();
return View(applicationDbContext);
}
The error message explains the problem fairly clearly: you're passing in a different type than the view is expecting.
Specifically, you call GetAllBarH() to get the data for the view, and it returns IEnumerable<TaBarHeader>. Therefore the model declaration for the page should be:
#model IEnumerable<TraficAlert.Models.TaBarHeader>
If you really wanted HeaderTelegramaViewModel then you're going to have to convert the IEnumerable<TaBarHeader> somehow. I assume you missed that step in your controller.
Why do I get the above mentioned error?
Because the data type returned in your action is not the same as the data type required on the view.
You can modify your HeaderTelegramaController like this:
public IActionResult Index()
{
var applicationDbContext = _unitofwork.BarHeader.GetAllBarH().Select(m => new HeaderTelegramaViewModel { TaBarHeader = m }).ToList();
return View(applicationDbContext);
}
A possible error I assume may come from your table head
try specifying an index considering your model is an IEnumerable.
So change
#Html.DisplayFor(modelItem => item.TaBarHeader.Id)
to something like this
#Html.DisplayFor(modelItem => item[0].TaBarHeader.Id)

How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework

I have written a linq query from which I would like to obtain the values from that query and display the results in a table. I keep getting an error that reads as shown below
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType56[System.Int32,System.String,System.String,System.String,System.String,System.Nullable1[System.DateTime]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[App.Web.marketingdbclients_invalidEmails]'
When I scaffolded the view from the controller I picked one of my tables as the model and I don't think that is the right way to do it. What is the right type of model to pick if I want to display results from a linq query and or stored procedure because I also have a stored procedure that does the same thing as the linq query so perhaps I could use the sp as an alternative
public class InvalidEmailsController : Controller
{
private MarketingDBEntitiesModel db = new MarketingDBEntitiesModel();
public ActionResult InvalidEmails(int UploadId)
{
//var emails = db.sp_marketing_getInvalidEmailsByUploadId(UploadId);
var emailTable = (from mie in db.marketingdbclients_invalidEmails
join mdt in db.marketingdbclients_dataTable on mie.ClientId equals mdt.ClientId
where mdt.UploadId == 88
select new
{
mie.ClientId, mie.Email1, mie.Email2,
mie.Email3, mie.Email4, mie.DateStamp
});
return View(emailTable);
}
}
This is my view which is bound to a a table as model (which I think is the wrong method)
#model IEnumerable<App.Web.marketingdbclients_invalidEmails>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.ClientId)
</th>
<th>
#Html.DisplayNameFor(model => model.Email1)
</th>
<th>
#Html.DisplayNameFor(model => model.Email2)
</th>
<th>
#Html.DisplayNameFor(model => model.Email3)
</th>
<th>
#Html.DisplayNameFor(model => model.Email4)
</th>
<th>
#Html.DisplayNameFor(model => model.DateStamp)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.ClientId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email2)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email3)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email4)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateStamp)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.invalidEmailId }) |
#Html.ActionLink("Details", "Details", new { id=item.invalidEmailId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.invalidEmailId })
</td>
</tr>
}
</table>
You are trying to pass an anonymous type to 'App.Web.marketingdbclients_invalidEmails' so that's the way it produces an error.
You need to create a new View Model with required properties that you want to project from LINQ and show in your view.
public class marketingdbclients_invalidEmailsVM
{
public int ClientId { get; set; }
public string Email1 { get; set; }
public string Email2 { get; set; }
public string Email3 { get; set; }
public string Email4 { get; set; }
public DateTime DateStamp { get; set; }
}
Now use this view model for your LINQ
public ActionResult InvalidEmails(int UploadId)
{
//var emails = db.sp_marketing_getInvalidEmailsByUploadId(UploadId);
var emailTable = (from mie in db.marketingdbclients_invalidEmails
join mdt in db.marketingdbclients_dataTable on mie.ClientId equals mdt.ClientId
where mdt.UploadId == 88
select new marketingdbclients_invalidEmailsVM
{
ClientId = mie.ClientId,
Email1=mie.Email1,
Email2=mie.Email2,
Email3=mie.Email3,
Email4=mie.Email4,
DateStamp=mie.DateStamp
});
return View(emailTable);
}
And bind your model using a new created View Model.
#model IEnumerable<App.Web.marketingdbclients_invalidEmailsVM>
Hopefully, it will resolve your problem.
If you have a model in view:
#model IEnumerable<App.Web.marketingdbclients_invalidEmails>
You must return the same type from the controller action.
Change this:
... select new {...});
to this:
... select new App.Web.marketingdbclients_invalidEmails {...}).ToList();
and model to:
#model List<App.Web.marketingdbclients_invalidEmails>
Good luck

Enable boolean and enter text in view then pass back to controller - MVC

I have a view that displays a boolean (currently defaulted to 0) in a box format in the view that I cannot check to activate as true and also want to enter text in the result field to pass back to the controller and save both changes to a table. Can someone please explain what I have to do to allow this functionality to work please.
Controller code
public ActionResult P1A1Mark()
{
List<MarkModel> query = (from row in db.submits
where row.assignment_no.Equals("1") && row.group_no == 1
group row by new { row.assignment_no, row.student_no, row.student.firstname, row.student.surname } into g
select new MarkModel
{
student_no = g.Key.student_no,
student_surname = g.Key.surname,
student_firstname = g.Key.firstname
}
).ToList();
return View(query);
}
View
#model IEnumerable<MvcApplication2.Models.MarkModel>
#{
ViewBag.Title = "P1A1Mark";
}
<h2>Mark Student Assignments</h2>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.student_no)
</th>
<th>
#Html.DisplayNameFor(model => model.student_surname)
</th>
<th>
#Html.DisplayNameFor(model => model.student_firstname)
</th>
<th>
#Html.DisplayNameFor(model => model.submitted)
</th>
<th>
#Html.DisplayNameFor(model => model.result)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.student_no)
</td>
<td>
#Html.DisplayFor(modelItem => item.student_surname)
</td>
<td>
#Html.DisplayFor(modelItem => item.student_firstname)
</td>
<td>
#Html.DisplayFor(modelItem => item.submitted)
</td>
<td>
#Html.DisplayFor(modelItem => item.result)
</td>
</tr>
}
</table>
Model
public class MarkModel
{
public string student_no { get; set; }
public string student_surname { get; set; }
public string student_firstname { get; set; }
public string assignment_no { get; set; }
public bool submitted { get; set; }
public string result { get; set; }
public Nullable<int> group_no { get; set; }
}
Create an EditorTemplate for type of MarkModel.
In /Views/Shared/EditorTemplates/MarkModel.cshtml
#model MvcApplication2.Models.MarkModel
<tr>
<td>#Html.DisplayFor(m => m.student_no)</td>
<td>#Html.DisplayFor(m => m.student_surname)</td>
<td>#Html.DisplayFor(m => m.student_firstname)</td>
<td>#Html.CheckBoxFor(m => m.submitted)</td>
<td>#Html.TextBoxFor(m => m.result)</td>
</tr>
and in the main view
#model IEnumerable<MvcApplication2.Models.MarkModel>
#using (Html.BeginForm())
{
<table>
<thead>
// add your th elements
</thead>
<tbody>
#Html.EditorFor(m => m)
<tbody>
</table>
<input type="submit" ../>
}
and create a method to post back to
[HttpPost]
public ActionResult P1A1Mark(IEnumerable<MarkModel>model)
Alternatively you can use a for loop in the view (the model must be IList<T>)
for(int i = 0; i < Model.Count; i++)
{
....
#Html.CheckBoxFor(m => m[i].submitted)
}

MVC5 Linq to ViewModel to Razor View code improvement?

I want to fetch data from two EF tables (Menus and MenuItems). Then I would like to place the result into a ViewModel. So I can easily loop through the data in the Razor View.
I've got it working. But I'm new to .Net MVC so I was wondering if my code could be improved. Maybe the queries can be merged and a lot shorter. And I'm using AsEnumerable(), I'm not sure if that is the best approach.
I think my attempt is okay. But I would like to hear what you think. Any suggestion for code improvement is welcome. Thanks.
The controller class:
public ActionResult Index(int? id)
{
if (id == null) return RedirectToAction("Index", new { controller = "Menu" });
var menu =
(from m in _db.Menus
join mi in _db.MenuItems on m.Id equals mi.MenuId
where m.Id == id
select m).First();
var menuItems =
(from mi in _db.MenuItems.AsEnumerable()
join m in _db.Menus on mi.MenuId equals m.Id
where m.Id == id
select new MenuItem
{
Id = mi.Id,
Name = mi.Name,
Href = mi.Href,
CssClass = mi.CssClass,
CssId = mi.CssId,
Title = mi.Title,
Weight = mi.Weight
});
var model = new MenuModelView
{
Id = menu.Id,
Name = menu.Name,
CssClass = menu.CssClass,
CssId = menu.CssId,
Deleted = menu.Deleted,
MenuItems = menuItems
};
return View(model);
}
The ViewModel class:
using System.Collections.Generic;
namespace DesignCrew.Areas.Admin.Models
{
public class MenuModelView
{
public int Id { get; set; }
public string Name { get; set; }
public string CssClass { get; set; }
public string CssId { get; set; }
public bool Deleted { get; set; }
public IEnumerable<MenuItem> MenuItems { get; set; }
}
}
The Razor View:
#model DesignCrew.Areas.Admin.Models.MenuModelView
#{
ViewBag.Title = "Index";
}
<h2>Menu - #Html.DisplayFor(model => model.Name, new { #class = "control-label col-md-2" })</h2>
<p>
#Html.ActionLink("Create New Item", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().Id)
</th>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().Name)
</th>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().Href)
</th>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().Title)
</th>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().CssClass)
</th>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().CssId)
</th>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().ParentId)
</th>
<th>
#Html.DisplayNameFor(model => model.MenuItems.First().Weight)
</th>
<th>Options</th>
</tr>
#foreach (var item in Model.MenuItems)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Href)
</td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.CssClass)
</td>
<td>
#Html.DisplayFor(modelItem => item.CssId)
</td>
<td>
#Html.DisplayFor(modelItem => item.ParentId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Weight)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { #class = "link-menu" }) |
#Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { #class = "link-menu" })
</td>
</tr>
}
</table>
If your EF model has a navigable foreign key between Menu and MenuItem, you won't need to explicitly join the two tables, and you can eager load the child MenuItems at the same time you fetch the parent Menu, and simply navigate from parent to child:
var menu = _db.Menus
.Include("MenuItems") // Or, use the typed version on newer EF's
.First(m => m.id == id); // Many LINQ expressions allow predicates
return new MenuModelView
{
Menu = menu.Name,
CssClass = menu.CssClass,
CssId = menu.CssId,
Deleted = menu.Deleted,
MenuItems = menu.MenuItems
}
And, since there seems to be much commonality in the MenuModelView vs the Menu EF entity, you could look at using AutoMapper. Once configured, this would allow you to replace the manual mapping step with something like:
return Mapper.Map<Menu, MenuModelView>(menu);
Edit
Here's a cheap and nasty ViewModel, which wraps your EF entities, and augments it with presentation tier data. Purists would probably note that you should create new classes for the wrapped EF Model, although then again, your EF model seems to model Html :)
public class MenuModelView
{
// Presentation tier stuff
public string PageTitle { get; set; }
public string MetaTagsForSEO { get; set; }
public bool IsThisARegisteredUserSoSkipTheAdverts { get; set; }
// Your EF / Domain Model
public Menu Menu { get; set; }
}
Your razor #Model is the MenuModelView.

MVC return list of items to the view

Can someone please help me out. I am trying to retrieve a list of storage details from the database and simply display the list in a view.
Storage Model:
public class StorageModel
{
[Required]
[Display(Name = "Storage Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Date From")]
public string DateFrom { get; set; }
[Required]
[Display(Name = "Date To")]
public string DateTo { get; set; }
[Required]
[Display(Name = "Size")]
public string Size { get; set; }
}
Controller Method:
public ActionResult ViewStorage()
{
List<CommonLayer.TblNewsStorage> storageList = new BusinessLayer.Storage().getAllStorage().ToList();
return View(storageList);
}
Data being retrieved from the BusinessLayer above:
public IQueryable<CommonLayer.TblNewsStorage> getAllStorage()
{
return this.Entities.TblNewsStorage;
}
Now I created a strongly typed view with the StorageModel using view scaffold template, however it is not working. What exactly am I doing wrong? I tried passing a var instead of a List but still it is not working. I am new to MVC so I must be doing something wrong. Which is the proper way to pass and display a list of data to a view?
View code generated by MVC:
#model IEnumerable<NewsLibrary.Models.StorageModel>
#{
ViewBag.Title = "ViewStorage";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ViewStorage</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.DateFrom)
</th>
<th>
#Html.DisplayNameFor(model => model.DateTo)
</th>
<th>
#Html.DisplayNameFor(model => model.Size)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateFrom)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateTo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Size)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
I get the following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[CommonLayer.TblNewsStorage]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[NewsLibrary.Models.StorageModel]'.
The
#model IEnumerable<NewsLibrary.Models.StorageModel>
must have the same datatype passed in.
You are passing in
List<CommonLayer.TblNewsStorage>
If you take a closer look at the Model the View expects you see it's IEnumerable of NewsLibrary.Models.StorageModel. You are passing a list/IEnumerable of the type CommonLayer.TblNewsStorage. Make sure these two are the same datatype.
#foreach (var item in Model) {
item.Name
}
try like this. You need to remove #html.displayfor(model => item.name) to only item.name inside your tags

Categories