asp.net-mvc


Can we display linq result in view without using model


I was trying to display the result without model(CLASS), I already have a department and Employee class, now to show their joined result I don't want to make another class which contains EmpName and Department Name property.
IN MODEL
public class EmpDetails
{
public int EmpID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int DepartmentID { get; set; }
}
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public string Location { get; set; }
}
IN CONTROLLER
var q = (from ED in cc.EmpDetails
join DT in cc.Department
on ED.DepartmentID equals DT.DepartmentID
select new {
ED.Name,
DT.DepartmentName
});
ViewBag.ListData = q;
IN VIEW
ERROR :: 'object' does not contain a definition for 'Name'
foreach (var item in ViewBag.ListData)
{
<div>
#item.Name
</div>
<div>
#item.DepartmentName
</div>
}
I want it to be done without using model
You could try something like this
#model dynamic
<h1>List of Customers</h1>
<div>
#foreach(var item in Model.Customers)
{
<h2>#item.CustomerID</h2>
}
</div>
Sample Query
var query = from c in db.Customers
join o in db.Orders
on c.CustomerID equals o.CustomerID
orderby c.CustomerID ascending
select new {
c.CustomerID,
c.CompanyName,
o.OrderID,
o.OrderDate
};
Here, you are not selecting Customer objects (select c).
You are creating an anonymous type that has four properties - CustomerID, CompanyName, OrderID and OrderDate. Although this LINQ query works without any problem.
but you can't pass anonymous types to an ASP.NET MVC view.
A recommended solution but again you need create an additional class. If you wish to avoid doing that for some reason, here also ExpandoObject.
You can store the realized data in an ExpandoObject and then pass it
to the view.
public ActionResult Index()
{
NorthwindEntities db=new NorthwindEntities();
dynamic model = new ExpandoObject();
model.Customers = db.Customers.ToList();
model.Orders = db.Orders.ToList();
return View(model);
}
See more details here
Is there any particular reason you don't want to use model? You can try using ViewModel instead , it is still like a model but it is use when you have specific data that you want to display on view. You can write a representation of Department and Employment class like this
ViewModel:
public class DepartmentEmploymentViewModel
{
public string departmentName { get; set; }
public string employmentName { get; set; }
}
Controller:
var q = (from ED in cc.EmpDetails
join DT in cc.Department
on ED.DepartmentID equals DT.DepartmentID
select new {
ED.Name,
DT.DepartmentName
});
//collection of department and employment name
List<DepartmentEmploymentViewModel> dpVM = new List<DepartmentEmploymentViewModel>();
dpVM = q;
View:
#model projName.Models.DepartmentEmploymentViewModel
foreach (var item in Model)
{
<div>
#item.Name
</div>
<div>
#item.DepartmentName
</div>
}
You could select your values as an array of strings.
CONTROLLER - remember to use ToList() on the last line:
var q = from ED in cc.EmpDetails
join DT in cc.Department
on ED.DepartmentID equals DT.DepartmentID
select new { ED.Name, DT.Departmentname };
ViewBag.ListData = (from l in select new string[] { Name, Departmentname}).ToList();
Then in your view reference your values using the array index.
VIEW:
foreach (string[] item in ViewBag.ListData)
{
<div>
#item[0]
</div>
<div>
#item[1]
</div>
}

Related Links

#Html.DisplayFor() value not change after the postback
Location of Validation Message
ASP.NET Identity update column type for primary key Id in AspNetUsers table from a string to and int or a guid, yes or no?
Deploying ASP MVC 5 App with IIS 7.5
validation for dropdown in MVC3
MVC getting a list of music and image into a table
How to allow users to only edit their details in a catalog
Defining Repository for MVC best practice
MVC ASP.NET search filter by int
How do I add placeholder text from the model into a MVC view?
Web site analytic application in the mvc application
mvc3 EditorFor/DisplayFor throws Collection was modified; enumeration operation may not execute
When I update ASP.NET MVC from 5.0 to 5.1, do I need to update the bindingRedirect to 5.1?
Set ClaimsAuthorizationManager in Code (MVC 5)
KendoUi MVC Grid passing parameter values
How exactly does CSS/Javascript minification and bundling work in MVC?

Categories

HOME
apache-spark
ssl
f#
oozie
logstash
opencl
hortonworks-data-platform
serial-port
load
compiler-errors
tcl
python-3.5
linear-programming
swashbuckle
turing
encog
wikipedia-api
r-caret
intl
sparkpost
nservicebus
reload
apache-kafka-streams
jquery-selectors
org-mode
javers
lexikjwtauthbundle
multiprocessing
cayley
powershell-v2.0
similarity
sencha-touch-2
liferay-aui
ratchet-2
padding
spring-batch-admin
memorystream
sql-server-2014-express
true-type-fonts
lync
ose
stackexchange.exceptional
tweenmax
codeskulptor
pymssql
phing
powerbuilder-conversion
confirm
transpose
sinon
message-hub
easy-digital-downloads
mamp-pro
stateful
ulimit
sweet.js
backwards-compatibility
invalidate
slt
autofocus
zuul
jquery-masonry
android-identifiers
graph-traversal
evaluate
proc
vb4android
finalcut
activemodel
target-platform
docklight
membership
oracle-bi
uac
text-alignment
hypervisor
multiscreen
ppl
sketching
bonfire
access-log
textmatching
knockout-mvc
citymaps
mui
keyboard-navigation
asp.net-apicontroller
modalpopupextender
keymapping
css3pie
notifyicon
ramdisk
couchrest
libtorrent-rasterbar
sugar.js
sparc
kmz
progressdialog
libavg
51degrees
onmouseout
server.mappath
ed
payment-services
ramaze
having
directoryentry
pastebin
custom-protocol
xml-database
anemic-domain-model
crc-cards
reference-library

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App