TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Madhav
NA
196
43.3k
How to pass multiple models to view in MVC in .net core 2.0
Jun 4 2018 4:35 AM
Here i want to pass data from multiple tables to MVC view in .net core 2.0.
here i have some ms sql tables which are dependant,
these are like below.
1. Organizations
2. Primary Contact Information
3. Regions.
4. Renewals.
1.Orgnization table have Organization ID(PK), Name.. and Region ID(FK from regions).
2.Primary contact has First name,Last name .... and organization ID(FK from organizations)
3.Regions has Region ID,Name and Description,
4.Renewals has Renewal ID(PK)..... and Organization ID( FK from organizations).
and here I am passing this renewals data to VIEW here I wanto show,
Organization Name,Fist and Last name, and Region name in MVC view in table format.
to accomplish above functionality i have written below code.
IEnumerable model =
new
List();
try
{
model = context.Set().ToList().Select(s =>
new
AllRenewalsVM
{
OrganizationName = getOrgnizationName(s.OrgnizationId),
RenewalId = s.RenewalId,
ContactPersonName = getName(s.OrgnizationId),
RegionName = getRegion(s.OrgnizationId),
});
return
View(
"AllRenewals"
, model);
public
string
getOrgnizationName(Guid orgnizationId)
{
string
orgName =
""
;
try
{
var orgnizationName = context.
set
.Where(s => s.OrgnizationId == orgnizationId).FirstOrDefault();
if
(orgnizationName.IsDeleted ==
null
)
{
orgName = orgnizationName.Name.ToString();
}
}
catch
(Exception e)
{
}
return
orgName;
}
public
string
getName(Guid orgnizationId)
{
string
Name =
""
;
try
{
var Names = context.
set
().Where(s => s.OrgnizationId == orgnizationId).FirstOrDefault();
orgnizationId).FirstOrDefault();
if
(Names.IsDeleted ==
null
)
{
Name = Names.FirstName +
" "
+ Names.LastName.ToString();
}
}
catch
(Exception e)
{
}
return
Name;
}
public
string
getRegion(Guid orgnizationId)
{
string
Name =
""
;
try
{
var org = context.Set().Where(s => s.OrgnizationId == orgnizationId).FirstOrDefault();
var regionName = context.Set().Where(s => s.RegionId == org.RegionId).FirstOrDefault();
if
(regionName !=
null
)
{
Name = regionName.Name.ToString();
}
}
catch
(Exception e)
{
}
return
Name;
}
@* Including Model to VIEW *@
@model IEnumerable
@{
ViewData[
"Title"
] =
"Renewals"
;
Layout =
"~/Views/Shared/_Layout.cshtml"
;
}
@* Binding data to HTML *@
class
=
"table table-striped dt-responsive display"
>
Name
Organization
Region
@foreach (
var
item
in
Model)
{
@Html.DisplayFor(modelItem => item.ContactPersonName)
@Html.DisplayFor(modelItem => item.OrganizationName)
@Html.DisplayFor(modelItem => item.RegionName)
}
This is my code which i used to show data on page,
this code is working very fine but it couses skow performance issue.
is there are any better way to pass data from multiple dependent table?
Reply
Answers (
1
)
Submit value without refresh page in mvc
c# jagged arrays