Today, in this article you will learn to use Partial View in a different manner in any MVC application. As you know, Partial View is a view that will be rendered on a parent view. So, I will show you with the help of this article that how we can render partial view in a different way on a parent view.

Partial View can be a parent view and we can use it as a parent view of any partial view. We can simply pass Model to show data in the partial view or we can send the data in the partial view with the help of AJAX call.

We will use Partial View to show the data as in jQuery UI dialogue and we will put Partial View in a div to show the data. In my Previous article on Paging, Searching, Filtering in MVC 5 with Partial View , we saw that how we can use Partial View to show the data in MVC Grid.

So, let’s get started and learn to use Partial View in MVC Application with the help of following structure:

  • Creating ASP.NET MVC Application
  • Performing Database Operation
  • Working with Web Application

Creating ASP.NET MVC Application

In this section, we will create the MVC application. I am creating MVC 5 application and you can use it on MVC 3 or MVC 4. MVC 5 application can be created through Visual Studio 2013 or Visual Studio 2015. Let’s begin with the following steps:

Step 1: In the Visual Studio 2013, click on “New Project”,

new
Figure 1: Creating New Project in VS 2013

Step 2: Select the Web from the left pane and click on “ASP.NET Web Application” and enter the app name as “BestMovies”,

BestMovies
Figure 2: Creating Web App in VS 2013

Step 3: Select the MVC Project Template in the next “One ASP.Net” Wizard,

Wizard
Figure 3: MVC Template in VS 2013

Performing Database Operation

In this section we will create the database and table for performing data operation so that web application can fetch the data from the database. Start with the following steps:

Step 1: Just Create the database from the following code:

  1. CREATE DATABASE BestMovies

Step 2: Now run the following script to perform the database operations:

  1. USE [BestMovies]
  2. GO
  3. /****** Object: Table [dbo].[Actor] Script Date: 4/29/2016 2:47:20 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[Actor](
  11. [ActorInfoId] [int] IDENTITY(1,1) NOT NULL,
  12. [Name] [varchar](50) NULL,
  13. [Age] [int] NULL,
  14. [DOB] [datetime] NULL,
  15. CONSTRAINT [PK_Actor] PRIMARY KEY CLUSTERED
  16. (
  17. [ActorInfoId] ASC
  18. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  19. ) ON [PRIMARY]
  20. GO
  21. SET ANSI_PADDING OFF
  22. GO
  23. /****** Object: Table [dbo].[Movie] Script Date: 4/29/2016 2:47:20 PM ******/
  24. SET ANSI_NULLS ON
  25. GO
  26. SET QUOTED_IDENTIFIER ON
  27. GO
  28. SET ANSI_PADDING ON
  29. GO
  30. CREATE TABLE [dbo].[Movie](
  31. [ID] [int] IDENTITY(1,1) NOT NULL,
  32. [Name] [varchar](50) NULL,
  33. [Genre] [varchar](50) NULL,
  34. [ReleasedDate] [datetime] NULL,
  35. [Actor] [varchar](50) NULL,
  36. [Actress] [varchar](50) NULL,
  37. CONSTRAINT [PK_Movie] PRIMARY KEY CLUSTERED
  38. (
  39. [ID] ASC
  40. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  41. ) ON [PRIMARY]
  42. GO
  43. SET ANSI_PADDING OFF
  44. GO
  45. /****** Object: Table [dbo].[MovieInfo] Script Date: 4/29/2016 2:47:20 PM ******/
  46. SET ANSI_NULLS ON
  47. GO
  48. SET QUOTED_IDENTIFIER ON
  49. GO
  50. SET ANSI_PADDING ON
  51. GO
  52. CREATE TABLE [dbo].[MovieInfo](
  53. [MovieInfoId] [int] IDENTITY(1,1) NOT NULL,
  54. [MovieId] [int] NULL,
  55. [Director] [varchar](150) NULL,
  56. [Production] [nvarchar](150) NULL,
  57. [ImdbRating] [decimal](2, 1) NULL,
  58. [FilmfareAward] [int] NULL,
  59. [LeadRole] [varchar](50) NULL,
  60. CONSTRAINT [PK_MovieInfo] PRIMARY KEY CLUSTERED
  61. (
  62. [MovieInfoId] ASC
  63. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  64. ) ON [PRIMARY]
  65. GO
  66. SET ANSI_PADDING OFF
  67. GO
  68. SET IDENTITY_INSERT [dbo].[Actor] ON
  69. GO
  70. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (1, N'Amitabh Bachhan', 73, CAST(N'1942-11-10 00:00:00.000' AS DateTime))
  71. GO
  72. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (2, N'Rajesh Khanna', 73, CAST(N'1942-12-29 00:00:00.000' AS DateTime))
  73. GO
  74. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (3, N'Shahrukh Khan', 50, CAST(N'1965-02-12 00:00:00.000' AS DateTime))
  75. GO
  76. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (4, N'Anil Kapoor', 59, CAST(N'1956-12-24 00:00:00.000' AS DateTime))
  77. GO
  78. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (5, N'Aishwarya', 42, CAST(N'1973-12-01 00:00:00.000' AS DateTime))
  79. GO
  80. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (6, N'Akshay Kumar', 48, CAST(N'1967-09-09 00:00:00.000' AS DateTime))
  81. GO
  82. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (7, N'Dilip Kumar', 93, CAST(N'1922-12-11 00:00:00.000' AS DateTime))
  83. GO
  84. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (8, N'Amir Khan', 51, CAST(N'1965-03-14 00:00:00.000' AS DateTime))
  85. GO
  86. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (9, N'Farhan Akhtar', 42, CAST(N'1942-01-09 00:00:00.000' AS DateTime))
  87. GO
  88. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (10, N'Saif Ali Khan', 45, CAST(N'1970-08-16 00:00:00.000' AS DateTime))
  89. GO
  90. INSERT [dbo].[Actor] ([ActorInfoId], [Name], [Age], [DOB]) VALUES (11, N'Prabhas', 36, CAST(N'1979-10-23 00:00:00.000' AS DateTime))
  91. GO
  92. SET IDENTITY_INSERT [dbo].[Actor] OFF
  93. GO
  94. SET IDENTITY_INSERT [dbo].[Movie] ON
  95. GO
  96. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (1, N'Sholay', N'Action', CAST(N'1975-08-15 00:00:00.000' AS DateTime), N'Amitabh, Dharmendar', N'Hema Malini')
  97. GO
  98. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (2, N'Deewar', N'Action', CAST(N'1979-05-14 00:00:00.000' AS DateTime), N'Amitabh, Shashi', N'Parveen Boby')
  99. GO
  100. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (3, N'Zanzeer', N'Action', CAST(N'1973-05-11 00:00:00.000' AS DateTime), N'Amitabh', N'Jaya Bhaduri')
  101. GO
  102. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (4, N'Don', N'Action', CAST(N'1978-04-20 00:00:00.000' AS DateTime), N'Amitabh', N'Zeenat Aman')
  103. GO
  104. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (5, N'Anand', N'Drama', CAST(N'1971-04-03 00:00:00.000' AS DateTime), N'Rajesh Khanna', N'Sumita Snyal')
  105. GO
  106. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (6, N'Bawarchi', N'Drama', CAST(N'1972-06-10 00:00:00.000' AS DateTime), N'Rajesh Khanna', N'Jaya Bhaduri')
  107. GO
  108. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (7, N'D D L J', N'Romantic', CAST(N'1995-08-19 00:00:00.000' AS DateTime), N'Shahrukh Khan', N'Kajol')
  109. GO
  110. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (8, N'Kuch Kuch Hota Hai', N'Romantic', CAST(N'1998-08-16 00:00:00.000' AS DateTime), N'Shahrukh Khan', N'Kajol')
  111. GO
  112. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (9, N'Nayak', N'Action', CAST(N'2001-07-07 00:00:00.000' AS DateTime), N'Anil Kapoor', N'Rani Mukharjee')
  113. GO
  114. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (10, N'Taal', N'Drama', CAST(N'1999-08-13 00:00:00.000' AS DateTime), N'Anil Kapoor', N'Aishwarya Rai')
  115. GO
  116. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (11, N'Sainik', N'Action', CAST(N'1993-07-10 00:00:00.000' AS DateTime), N'Akshay Kumar', N'Ashwini Bhave')
  117. GO
  118. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (12, N'Karma', N'Action', CAST(N'1986-08-08 00:00:00.000' AS DateTime), N'Dilip Kumar', N'Nutan')
  119. GO
  120. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (13, N'Sarfarosh', N'Action', CAST(N'1995-08-08 00:00:00.000' AS DateTime), N'Amir Khan', N'Sonali Bendre')
  121. GO
  122. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (14, N'Saudagar', N'Action', CAST(N'1999-09-12 00:00:00.000' AS DateTime), N'Dilip Kumar, Raj Kumar', N'Manish Koirala')
  123. GO
  124. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (15, N'Three Idiots', N'Drama', CAST(N'2012-09-09 00:00:00.000' AS DateTime), N'Amir Khan', N'Kareena Kapoor')
  125. GO
  126. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (16, N'Rowdy Rathore', N'Action', CAST(N'2013-09-10 00:00:00.000' AS DateTime), N'Akshay Kumar', N'Sonakshi Sinha')
  127. GO
  128. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (17, N'Baby', N'Action', CAST(N'2015-12-12 00:00:00.000' AS DateTime), N'Akshay Kumar', N'Tapsi')
  129. GO
  130. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (18, N'Bhaag Milka Bhaag', N'Biographic', CAST(N'2014-10-10 00:00:00.000' AS DateTime), N'Farhan Akhtar', N'Sonam Kapoor')
  131. GO
  132. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (19, N'Phantom', N'Action', CAST(N'2015-08-12 00:00:00.000' AS DateTime), N'Saif Ali Khan', N'Kareena Kapoor')
  133. GO
  134. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (20, N'Airlift', N'Action', CAST(N'2016-05-06 00:00:00.000' AS DateTime), N'Akshay Kumar', N'Nimrat Kaur')
  135. GO
  136. INSERT [dbo].[Movie] ([ID], [Name], [Genre], [ReleasedDate], [Actor], [Actress]) VALUES (21, N'Bahubali', N'Action', CAST(N'2015-08-12 00:00:00.000' AS DateTime), N'Prabhas', N'Tamannah Bhatiya')
  137. GO
  138. SET IDENTITY_INSERT [dbo].[Movie] OFF
  139. GO
  140. SET IDENTITY_INSERT [dbo].[MovieInfo] ON
  141. GO
  142. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (1, 1, N'Ramesh Sippy', N'United Producers
  143. Sippy Films', CAST(8.5 AS Decimal(2, 1)), 5, N'Amitabh Bachhan')
  144. GO
  145. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (2, 2, N'Yash Chopra', N'Trimurti Films', CAST(8.2 AS Decimal(2, 1)), 3, N'Amitabh Bachhan')
  146. GO
  147. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (3, 3, N'Prakash Meshra', N'Asha Studios', CAST(7.0 AS Decimal(2, 1)), 2, N'Amitabh Bachhan')
  148. GO
  149. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (4, 4, N'Chandra Barot', N'Nariman Films', CAST(7.9 AS Decimal(2, 1)), 3, N'Amitabh Bachhan')
  150. GO
  151. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (5, 5, N'Hrishikesh Mukherjee', N'Digital Entertainment', CAST(8.9 AS Decimal(2, 1)), 5, N'Rajesh Khanna')
  152. GO
  153. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (6, 6, N'Hrishikesh Mukherjee', N'Digital Entertainment', CAST(8.0 AS Decimal(2, 1)), 1, N'Rajesh Khanna')
  154. GO
  155. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (7, 7, N'Aditya Chopra', N'Yash Raj Films', CAST(8.3 AS Decimal(2, 1)), 3, N'Shahrukh Khan')
  156. GO
  157. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (8, 8, N'Karan Johar', N'Dharma Productions', CAST(7.8 AS Decimal(2, 1)), 1, N'Shahrukh Khan')
  158. GO
  159. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (9, 9, N'S. Shankar', N'Sri Surya Movies', CAST(7.8 AS Decimal(2, 1)), NULL, N'Anil Kapoor')
  160. GO
  161. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (10, 10, N'Subhash Ghai', N'Mukta Arts', CAST(6.8 AS Decimal(2, 1)), NULL, N'Aishwarya')
  162. GO
  163. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (11, 11, N'Sikander Bharti', N'Manish Arts', CAST(6.6 AS Decimal(2, 1)), NULL, N'Akshay Kumar')
  164. GO
  165. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (12, 12, N'Sikander Bharti', N'Mukta Arts', CAST(7.4 AS Decimal(2, 1)), NULL, N'Dilip Kumar')
  166. GO
  167. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (13, 13, N'John Matthew Matthan', N'Cinematt Pictures', CAST(8.2 AS Decimal(2, 1)), 2, N'Amir Khan')
  168. GO
  169. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (14, 14, N'Subhash Ghai', N'Mukta Arts', CAST(6.7 AS Decimal(2, 1)), NULL, N'Dilip Kumar')
  170. GO
  171. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (15, 15, N'Rajkumar Hirani', N'Vinod Chopra Films', CAST(8.4 AS Decimal(2, 1)), 3, N'Amir Khan')
  172. GO
  173. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (16, 16, N'Prabhu Deva', N'SLB Films', CAST(5.8 AS Decimal(2, 1)), NULL, N'Akshay Kumar')
  174. GO
  175. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (17, 17, N'Neeraj Pandey', N'T-Series', CAST(8.2 AS Decimal(2, 1)), 2, N'Akshay Kumar')
  176. GO
  177. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (18, 18, N'Rakeysh Omprakash Mehra', N'ROMP Pictures', CAST(8.3 AS Decimal(2, 1)), 3, N'Farhan Akhtar')
  178. GO
  179. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (19, 19, N'Kabir Khan', N'Nadiadwala Grandson Entertainment', CAST(5.6 AS Decimal(2, 1)), NULL, N'Saif Ali Khan')
  180. GO
  181. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (20, 20, N'Raja Krishna Menon', N'Abundantia Entertainment', CAST(9.1 AS Decimal(2, 1)), 2, N'Akshay Kumar')
  182. GO
  183. INSERT [dbo].[MovieInfo] ([MovieInfoId], [MovieId], [Director], [Production], [ImdbRating], [FilmfareAward], [LeadRole]) VALUES (21, 21, N'S. S. Rajamouli', N'
  184. Arka Media Works', CAST(8.6 AS Decimal(2, 1)), NULL, N'Prabhas')
  185. GO
  186. SET IDENTITY_INSERT [dbo].[MovieInfo] OFF
  187. GO
  188. ALTER TABLE [dbo].[MovieInfo] WITH CHECK ADD CONSTRAINT [FK_MovieInfo_Movie] FOREIGN KEY([MovieId])
  189. REFERENCES [dbo].[Movie] ([ID])
  190. GO
  191. ALTER TABLE [dbo].[MovieInfo] CHECK CONSTRAINT [FK_MovieInfo_Movie]
  192. GO
  193. /****** Object: StoredProcedure [dbo].[BM_GetActorDetails] Script Date: 4/29/2016 2:47:20 PM ******/
  194. SET ANSI_NULLS ON
  195. GO
  196. SET QUOTED_IDENTIFIER ON
  197. GO
  198. -- =============================================
  199. -- Author: Nimit
  200. -- Create date: 04/29/2016
  201. -- Description: get actor info
  202. -- =============================================
  203. CREATE PROCEDURE [dbo].[BM_GetActorDetails]
  204. -- Add the parameters for the stored procedure here
  205. @Name varchar(50)
  206. AS
  207. BEGIN
  208. -- SET NOCOUNT ON added to prevent extra result sets from
  209. -- interfering with SELECT statements.
  210. SET NOCOUNT ON;
  211. -- Insert statements for procedure here
  212. SELECT * FROM Actor WHERE Name = @Name
  213. END
  214. GO
  215. /****** Object: StoredProcedure [dbo].[BM_GetMovies] Script Date: 4/29/2016 2:47:20 PM ******/
  216. SET ANSI_NULLS ON
  217. GO
  218. SET QUOTED_IDENTIFIER ON
  219. GO
  220. -- =============================================
  221. -- Author: Nimit Joshi
  222. -- Create date: 02/19/2016
  223. -- Description: This sp is used to get data
  224. -- =============================================
  225. CREATE PROCEDURE [dbo].[BM_GetMovies]
  226. -- Add the parameters for the stored procedure here
  227. @PageNumber INT ,
  228. @PageSize INT
  229. AS
  230. BEGIN
  231. -- SET NOCOUNT ON added to prevent extra result sets from
  232. -- interfering with SELECT statements.
  233. SET NOCOUNT ON;
  234. -- Insert statements for procedure here
  235. SELECT * FROM dbo.Movie (NOLOCK) ORDER BY ID
  236. END
  237. GO
  238. /****** Object: StoredProcedure [dbo].[BM_GetMoviesInfo] Script Date: 4/29/2016 2:47:20 PM ******/
  239. SET ANSI_NULLS ON
  240. GO
  241. SET QUOTED_IDENTIFIER ON
  242. GO
  243. -- =============================================
  244. -- Author: Nimit Joshi
  245. -- Create date: 04/29/2016
  246. -- Description: This sp is used to get movie info
  247. -- =============================================
  248. CREATE PROCEDURE [dbo].[BM_GetMoviesInfo]
  249. -- Add the parameters for the stored procedure here
  250. @MovieId INT
  251. AS
  252. BEGIN
  253. -- SET NOCOUNT ON added to prevent extra result sets from
  254. -- interfering with SELECT statements.
  255. SET NOCOUNT ON;
  256. -- Insert statements for procedure here
  257. SELECT m.Name ,
  258. m.Genre ,
  259. mi.Director ,
  260. mi.Production ,
  261. mi.ImdbRating ,
  262. mi.FilmfareAward ,
  263. mi.LeadRole
  264. FROM Movie m ( NOLOCK )
  265. INNER JOIN dbo.MovieInfo mi ON m.ID = mi.MovieId
  266. WHERE m.ID = @MovieId
  267. END
  268. GO

That’s it for the database section.

Working with Web Application

In this section, we will create the architecture of web application and fetch the data from the database and view the data in Razor View and in the Partial View. We will display the data in the Partial view with the help of jQuery UI dialogue or in any simple div.

So, let’s begin with the following procedure:

Adding Model:

Step 1: In the Solution Explorer, right click on the solution and click on “Add New Project”,

Add
Figure 4: Adding New Project

Step 2: Select the “Class Library” and enter the name as “BestMoviesModel”,

BestMoviesModel
Figure 5: Adding Class Library Project

Step 3: Now add a class in this project as named “Movie”,

Movie
Figure 6: Adding Class

Step 4: Update the class code with the help of following code:

  1. namespace BestMoviesModel
  2. {
  3. public class Movie
  4. {
  5. #region Properties
  6. public int Id { get; set; }
  7. public string Name { get; set; }
  8. public string Genre { get; set; }
  9. public DateTime ReleasedDate { get; set; }
  10. public string Actor { get; set; }
  11. public string Actress { get; set; }
  12. #endregion
  13. }
  14. public class MovieInfo
  15. {
  16. #region Properties
  17. public int MovieInfoId { get; set; }
  18. public string Director { get; set; }
  19. public string Production { get; set; }
  20. public decimal ImdbRating { get; set; }
  21. public int FilmfareAward { get; set; }
  22. public string LeadRole { get; set; }
  23. #endregion
  24. }
  25. public class Actor
  26. {
  27. #region Properties
  28. public int ActorInfoId { get; set; }
  29. public string Name { get; set; }
  30. public int Age { get; set; }
  31. public DateTime DOB { get; set; }
  32. #endregion
  33. }
  34. }

Step 5: Just build the solution.

Adding Core

In this section, we will add the project which will handle the database. Follow the steps below:

Step 1: In the Solution Explorer, right click on the solution and click on “Add New Project” and select “Class Library” as named “BestMoviesCore”

Step 2: Now add a reference of “BestMoviesModel” solution in this project,

reference
Figure 7: Adding Reference

Step 3: Now in the “BestMoviesCore” project, right click on the References and click on “Manage NuGet Packages” and search for “Enterprise Library” and install it in the project,

Packages
Figure 8: Adding Enterprise Library

Step 4: Now just create two folders as named “BL” and “DAL”.

Step 5: Now add a class in the DAL folder as named “MovieDAL” and replace the code with the following code:

  1. namespace BestMoviesCore.DAL
  2. {
  3. public class MovieDAL
  4. {
  5. #region Variable
  6. ///<summary>
  7. /// Specify the Database variable
  8. ///</summary>
  9. Database objDB;
  10. ///<summary>
  11. /// Specify the static variable
  12. ///</summary>
  13. static string ConnectionString;
  14. #endregion
  15. #region Constructor
  16. ///<summary>
  17. /// This constructor is used to get the connectionstring from the config file
  18. ///</summary>
  19. public MovieDAL()
  20. {
  21. ConnectionString = ConfigurationManager.ConnectionStrings["BestMovieConnectionString"].ToString();
  22. }
  23. #endregion
  24. #region Database Method
  25. public List<T> ConvertTo<T>(DataTable datatable) where T : new()
  26. {
  27. List<T> Temp = new List<T>();
  28. try
  29. {
  30. List<string> columnsNames = new List<string>();
  31. foreach (DataColumn DataColumn in datatable.Columns)
  32. columnsNames.Add(DataColumn.ColumnName);
  33. Temp = datatable.AsEnumerable().ToList().ConvertAll<T>(row => getObject<T>(row, columnsNames));
  34. return Temp;
  35. }
  36. catch
  37. {
  38. return Temp;
  39. }
  40. }
  41. public T getObject<T>(DataRow row, List<string> columnsName) where T : new()
  42. {
  43. T obj = new T();
  44. try
  45. {
  46. string columnname = "";
  47. string value = "";
  48. PropertyInfo[] Properties;
  49. Properties = typeof(T).GetProperties();
  50. foreach (PropertyInfo objProperty in Properties)
  51. {
  52. columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower());
  53. if (!string.IsNullOrEmpty(columnname))
  54. {
  55. value = row[columnname].ToString();
  56. if (!string.IsNullOrEmpty(value))
  57. {
  58. if (Nullable.GetUnderlyingType(objProperty.PropertyType) != null)
  59. {
  60. value = row[columnname].ToString().Replace("$", "").Replace(",", "");
  61. objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null);
  62. }
  63. else
  64. {
  65. value = row[columnname].ToString();
  66. objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(objProperty.PropertyType.ToString())), null);
  67. }
  68. }
  69. }
  70. }
  71. return obj;
  72. }
  73. catch (Exception ex)
  74. {
  75. return obj;
  76. }
  77. }
  78. #endregion
  79. #region Movie Details
  80. /// <summary>
  81. /// This method is used to get the movie data
  82. /// </summary>
  83. /// <param name="PageNumber"></param>
  84. /// <param name="PageSize"></param>
  85. /// <returns></returns>
  86. public List<Movie> GetMovieList(int? PageNumber, int? PageSize)
  87. {
  88. List<Movie> objGetMovie = null;
  89. objDB = new SqlDatabase(ConnectionString);
  90. using (DbCommand objcmd = objDB.GetStoredProcCommand("BM_GetMovies"))
  91. {
  92. try
  93. {
  94. objDB.AddInParameter(objcmd, "@PageNumber", DbType.Int32, PageNumber);
  95. objDB.AddInParameter(objcmd, "@PageSize", DbType.Int32, PageSize);
  96. using (DataTable dataTable = objDB.ExecuteDataSet(objcmd).Tables[0])
  97. {
  98. objGetMovie = ConvertTo<Movie>(dataTable);
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. throw ex;
  104. return null;
  105. }
  106. }
  107. return objGetMovie;
  108. }
  109. ///<summary>
  110. /// This method is used to get movie details by movie id
  111. ///</summary>
  112. ///<returns></returns>
  113. public List<MovieInfo> GetMovieInfoById(int Id)
  114. {
  115. List<MovieInfo> objMovieDetails = null;
  116. objDB = new SqlDatabase(ConnectionString);
  117. using (DbCommand objcmd = objDB.GetStoredProcCommand("BM_GetMoviesInfo"))
  118. {
  119. try
  120. {
  121. objDB.AddInParameter(objcmd, "@MovieId", DbType.Int32, Id);
  122. using (DataTable dataTable = objDB.ExecuteDataSet(objcmd).Tables[0])
  123. {
  124. objMovieDetails = ConvertTo<MovieInfo>(dataTable);
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. throw ex;
  130. return null;
  131. }
  132. }
  133. return objMovieDetails;
  134. }
  135. #endregion
  136. #region LeardRole Details
  137. ///<summary>
  138. /// This method is used to get the movie data
  139. ///</summary>
  140. ///<returns></returns>
  141. public List<Actor> GetLeadRoleDetails(string Name)
  142. {
  143. List<Actor> objGetLeadRoleActor = null;
  144. objDB = new SqlDatabase(ConnectionString);
  145. using (DbCommand objcmd = objDB.GetStoredProcCommand("BM_GetActorDetails"))
  146. {
  147. try
  148. {
  149. objDB.AddInParameter(objcmd, "@Name", DbType.String, Name);
  150. using (DataTable dataTable = objDB.ExecuteDataSet(objcmd).Tables[0])
  151. {
  152. objGetLeadRoleActor = ConvertTo<Actor>(dataTable);
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. throw ex;
  158. return null;
  159. }
  160. }
  161. return objGetLeadRoleActor;
  162. }
  163. #endregion
  164. }
  165. }

Step 6: Now add a class in the “BL” folder as named “MovieBL” and replace the code with the following code:

  1. namespace BestMoviesCore.BL
  2. {
  3. public class MovieBL
  4. {
  5. /// <summary>
  6. /// This method is used to get the movie data
  7. /// </summary>
  8. /// <param name="PageNumber"></param>
  9. /// <param name="PageSize"></param>
  10. /// <returns></returns>
  11. public List<Movie> GetMovieList(int? PageNumber, int? PageSize)
  12. {
  13. List<Movie> objGetMovie = null;
  14. try
  15. {
  16. objGetMovie = new MovieDAL().GetMovieList(PageNumber, PageSize);
  17. }
  18. catch (Exception)
  19. {
  20. throw;
  21. }
  22. return objGetMovie;
  23. }
  24. ///<summary>
  25. /// This method is used to get movie details by movie id
  26. ///</summary>
  27. ///<returns></returns>
  28. public List<MovieInfo> GetMovieInfoById(int Id)
  29. {
  30. List<MovieInfo> objMovieDetails = null;
  31. try
  32. {
  33. objMovieDetails = new MovieDAL().GetMovieInfoById(Id);
  34. }
  35. catch (Exception)
  36. {
  37. throw;
  38. }
  39. return objMovieDetails;
  40. }
  41. ///<summary>
  42. /// This method is used to get the movie data
  43. ///</summary>
  44. ///<returns></returns>
  45. public List<Actor> GetLeadRoleDetails(string Name)
  46. {
  47. List<Actor> objGetLeadRoleActor = null;
  48. try
  49. {
  50. objGetLeadRoleActor = new MovieDAL().GetLeadRoleDetails(Name);
  51. }
  52. catch (Exception)
  53. {
  54. throw;
  55. }
  56. return objGetLeadRoleActor;
  57. }
  58. }
  59. }

Step 7: That’s it with this section. Now build the solution.

Working with Web Application

Now in this section, we will work with the MVC web application and view the data. Start with the following steps:

Step 1: At first, we will add the reference of “BestMoviesModel” and “BestMoviesCore” projects reference in this project.

reference
Figure 9: Adding Reference in Web

Step 2: Right click on the Models folder and add a class as named “MovieDetails”,

MovieDetails
Figure 10: Adding Class in Models

Step 3: Replace the code with the following code:

  1. using BestMoviesModel;
  2. using System.Collections.Generic;
  3. namespace BestMovies.Models
  4. {
  5. public class MovieDetails
  6. {
  7. /// <summary>
  8. /// get and set the Movies
  9. /// </summary>
  10. public List<Movie> Movies { get; set; }
  11. }
  12. }

Step 4: Now right click on the Controllers folder and click on Add-> New -> Controller.

Step 5: Now in the wizard select the “MVC 5 Empty Controller”,

Controller
Figure 11: Add Scaffold in MVC 5

Step 6: Enter the controller name as “MovieController”,

MovieController
Figure 12: Add Controller in MVC 5

Step 7: Add the following method in the MovieController,

  1. /// <summary>
  2. /// This method is used to get all movies
  3. /// </summary>
  4. /// <param name="PageNumber"></param>
  5. /// <param name="PageSize"></param>
  6. /// <returns></returns>
  7. [HttpGet, ActionName("GetAllMovies")]
  8. public ActionResult GetAllMovies(int? PageNumber, int? PageSize)
  9. {
  10. List<Movie> objMovie = new List<Movie>();
  11. MovieBL objMovieBL = new MovieBL();
  12. if (object.Equals(PageNumber, null))
  13. {
  14. PageNumber = 1;
  15. }
  16. if (object.Equals(PageSize, null))
  17. {
  18. PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["DefaultPageSize"]);
  19. }
  20. objMovie = objMovieBL.GetMovieList(PageNumber, PageSize);
  21. return View("~/Views/Movie/BestMovies.cshtml", new MovieDetails() { Movies = objMovie });
  22. }

Step 8: Now goto Views-> Movie, right click on it and add view,

add
Figure 13: Adding View in MVC 5

Step 9: Enter the view name as “BestMovies”,

BestMovies
Figure 14: Add View Wizard

Step 10: Add the following code in the view,

  1. @model BestMovies.Models.MovieDetails
  2. @{
  3. ViewBag.Title = "BestMovies";
  4. }
  5. <h2>Best Movies</h2>
  6. <div class="MovieList">
  7. <div id="MoviesGrid">
  8. @Html.Partial("~/Views/Movie/_BestMoviesPartial.cshtml", Model.Movies)
  9. </div>
  10. </div>

Note: We are passing data to the Partial view to load the Movie data.

Step 11: Now add a Partial View as named “_BestMoviesPartial” in the View-> Movie folder,

View
Figure 15: Addding Partial View in MVC 5

Step 12: Now add the following code in this view:

  1. @model List<BestMoviesModel.Movie>
  2. <table class="table-responsive table">
  3. <thead>
  4. <tr>
  5. <th>Name</th>
  6. <th>Genre</th>
  7. <th>Released Date</th>
  8. <th>Actor</th>
  9. <th>Actress</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. @if (Model.Count > 0)
  14. {
  15. foreach (var movieItem in Model)
  16. {
  17. <tr>
  18. <td>@movieItem.Name</td>
  19. <td>@movieItem.Genre</td>
  20. <td>@movieItem.ReleasedDate.ToShortDateString()</td>
  21. <td>@movieItem.Actor</td>
  22. <td>@movieItem.Actress</td>
  23. </tr>
  24. }
  25. }
  26. else
  27. {
  28. <tr>
  29. <td>
  30. No Data Found
  31. </td>
  32. </tr>
  33. }
  34. </tbody>
  35. </table>

Step 13: Now build the solution and open the Views-> Shared-> _Layout.cshtml and replace the code with the highlighted code below:

  1. <head>
  2. <meta charset="utf-8" />
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>@ViewBag.Title - My Movies Application</title>
  5. @Styles.Render("~/Content/css")
  6. @Scripts.Render("~/bundles/modernizr")
  7. </head>
  8. <body>
  9. <div class="navbar navbar-inverse navbar-fixed-top">
  10. <div class="container">
  11. <div class="navbar-header">
  12. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  13. <span class="icon-bar"></span>
  14. <span class="icon-bar"></span>
  15. <span class="icon-bar"></span>
  16. </button>
  17. @Html.ActionLink("Best Movies", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
  18. </div>
  19. <div class="navbar-collapse collapse">
  20. <ul class="nav navbar-nav">
  21. <li>@Html.ActionLink("Home", "Index", "Home")</li>
  22. <li>@Html.ActionLink("About", "About", "Home")</li>
  23. <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
  24. <li>@Html.ActionLink("Movies", "GetAllMovies", "Movie")</li>
  25. </ul>
  26. @Html.Partial("_LoginPartial")
  27. </div>
  28. </div>
  29. </div>
  30. <div class="container body-content">
  31. @RenderBody()
  32. <hr />
  33. <footer>
  34. <p>© @DateTime.Now.Year - Best Movies Application</p>
  35. </footer>
  36. </div>
  37. @Scripts.Render("~/bundles/jquery")
  38. @Scripts.Render("~/bundles/bootstrap")
  39. @RenderSection("scripts", required: false)
  40. </body>
  41. </html>

Step 14: Now open the “Web.config” file of Web application and add the following two lines in your file:

Adding Key:

  1. <add key="DefaultPageSize" value="25" />
Adding ConnectionString:
  1. <add name="BestMovieConnectionString" connectionString="Data Source=MCNDESKTOP07;Initial Catalog=BestMovies;User Id = “UserID”;Password=”UserPassword”"
  2. roviderName="System.Data.SqlClient" />

Step 15: Now run the application and click on the “Movies”,

application
Figure 16: Main Page View of MVC

In the next page you will see Movies data,

Partial View
Figure 17: Partial View in MVC 5

Partial View with jQuery UI Dialogue

In this section, we will pass the Partial View in the jQuery UI Dialogue. For this please follow the steps below:

Step 1: Add the following method in the “MovieController”

  1. /// <summary>
  2. /// This method is used to get movie information
  3. /// </summary>
  4. /// <param name="MovieId"></param>
  5. /// <returns></returns>
  6. [HttpGet, ActionName("GetMovieInfo")]
  7. public ActionResult GetMovieInfo(string MovieId)
  8. {
  9. List<MovieInfo> ObjMovieInfo = new List<MovieInfo>();
  10. MovieBL objMovieBL = new MovieBL();
  11. ObjMovieInfo = objMovieBL.GetMovieInfoById(Convert.ToInt32(MovieId));
  12. return PartialView("~/Views/Movie/_MovieDetailsPartial.cshtml", new List<MovieInfo>(ObjMovieInfo));
  13. }

Step 2: Add another Partial View as named “_MovieDetailsPartial” in the Views->Movie folder and add the following code in it:

  1. @model List<BestMoviesModel.MovieInfo>
  2. <ul class="responsive-MovieDetails">
  3. @if (Model.Count > 0)
  4. {
  5. foreach (var item in Model)
  6. {
  7. <li class="UserDetailList"><span class="UserDetailHeader">Director</span><span>@item.Director</span></li>
  8. <li class="UserDetailList"><span class="UserDetailHeader">IMDB Rating</span><span>@item.ImdbRating</span></li>
  9. <li class="UserDetailList"><span class="UserDetailHeader">Production</span><span>@item.Production</span></li>
  10. <li class="UserDetailList"><span class="UserDetailHeader">Filmfare Awards</span><span>@item.FilmfareAward</span></li>
  11. <li class="UserDetailList"><span class="UserDetailHeader">Lead Role</span><span>@item.LeadRole</span></li>
  12. }
  13. }
  14. else
  15. {
  16. <li class="NoData">No data found</li>
  17. }
  18. </ul>

Step 3: Add the reference of jQueryUI from the NuGet Package Manager

reference
Figure 18: Adding jQuery UI NuGet Package

Step 4: Now open “BestMovies.cshtml” and add the following code at the end:

  1. <div class="popupcntr" id="movieInfo_content" style="display: none;" title="Movie Information">
  2. <div class="innerBox">
  3. <div id="MovieDetails"></div>
  4. </div>
  5. </div>

Step 5: Now open “_BestMoviesPartial.cshtml” and update the code with the highlighted code below:

  1. @model List<BestMoviesModel.Movie>
  2. <table class="table-responsive table">
  3. <thead>
  4. <tr>
  5. <th>Name</th>
  6. <th>Genre</th>
  7. <th>Released Date</th>
  8. <th>Actor</th>
  9. <th>Actress</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. @if (Model.Count > 0)
  14. {
  15. foreach (var movieItem in Model)
  16. {
  17. <tr>
  18. <td><a href="javascript:void(0)" onclick="GetMovieDetails('@movieItem.Id')">@movieItem.Name</a></td>
  19. <td>@movieItem.Genre</td>
  20. <td>@movieItem.ReleasedDate.ToShortDateString()</td>
  21. <td>@movieItem.Actor</td>
  22. <td>@movieItem.Actress</td>
  23. </tr>
  24. }
  25. }
  26. else
  27. {
  28. <tr>
  29. <td>
  30. No Data Found
  31. </td>
  32. </tr>
  33. }
  34. </tbody>
  35. </table>
  36. <script type="text/javascript">
  37. function GetMovieDetails(MovieId)
  38. {
  39. $('#movieInfo_content').dialog({
  40. dialogClass: 'moviedetail_dialog',
  41. modal: true,
  42. open: function (event, ui) {
  43. $.ajax({
  44. url: '@Url.Action("GetMovieInfo", "Movie")',
  45. dataType: "html",
  46. data: { MovieId: MovieId },
  47. type: "GET",
  48. error: function (xhr, status, error) {
  49. var err = eval("(" + xhr.responseText + ")");
  50. toastr.error(err.message);
  51. },
  52. success: function (data) {
  53. $("#divProcessing").hide();
  54. $('#MovieDetails').html(data);
  55. },
  56. beforeSend: function () {
  57. $("#divProcessing").show();
  58. }
  59. });
  60. },
  61. close: function (event, ui) { $('#movieInfo_content').dialog("destroy"); $('#MovieDetails').html(""); },
  62. });
  63. }
  64. </script>

Step 6: Now add the following css code in the “Site.css”,

  1. .moviedetail_dialog {
  2. padding: 20PX;
  3. background-color: #fbfbfb;
  4. border: 1px solid rgba(0,0,0,0.2);
  5. box-shadow: 0 0 6px black;
  6. }
  7. .ui-widget-header {
  8. display: inline-block;
  9. font-weight: bold;
  10. margin-right: 20px;
  11. }
  12. .moviedetail_dialog button {
  13. display: inline-block;
  14. margin-left: 20px;
  15. }
  16. .responsive-MovieDetails {
  17. list-style: none;
  18. margin-left: -41px;
  19. margin-top: 20px;
  20. }
  21. .responsive-MovieDetails:after {
  22. content: "";
  23. display: table;
  24. clear: both;
  25. }
  26. .UserDetailList {
  27. margin-bottom: 5px;
  28. margin: 0;
  29. }
  30. .UserDetailList:after {
  31. content: "";
  32. display: table;
  33. clear: both;
  34. }
  35. .UserDetailHeader {
  36. width: 118px;
  37. float: left;
  38. font-weight: bold;
  39. }
  40. .UserDetailHeader + span:before {
  41. content: ":";
  42. margin-right: 15px;
  43. position: absolute;
  44. left: -5px;
  45. }
  46. .UserDetailHeader + span {
  47. float: left;
  48. width: calc(100% - 118px);
  49. padding-left: 10px;
  50. position: relative;
  51. }
  52. #ActorContent .responsive-MovieDetails{
  53. border: 1px solid black; padding:2px;
  54. margin-left:0;
  55. }

Step 7: Now build the solution and run the application. Open the “Movies” page and just click on any Movie Name as shown below:

Movies
Figure 19: View in MVC 5

Step 8: You will show the popup in the main view as shown below:

popup
Figure 20: Partial View in UI dialogue

Now you can see that we have easily implemented and passed the Partial View in the jQuery UI dialogue.

Note: You have to add script tag of jQuery UI in the _Layout page.

Partial View with DIV Element

In this section we will load the Partial View in a div element. Start with the following steps:

Step 1: Add the following method in the “MovieController”,

  1. /// <summary>
  2. /// This method is used to get actor information
  3. /// </summary>
  4. /// <param name="MovieId"></param>
  5. /// <returns></returns>
  6. [HttpGet, ActionName("GetLeadRoleDetails")]
  7. public ActionResult GetLeadRoleDetails(string Name)
  8. {
  9. List<Actor> ObjActorInfo = new List<Actor>();
  10. MovieBL objMovieBL = new MovieBL();
  11. ObjActorInfo = objMovieBL.GetLeadRoleDetails(Name);
  12. return PartialView("~/Views/Movie/_ActorDetailsPartial.cshtml", new List<Actor>(ObjActorInfo));
  13. }

Step 2: Add another Partial View as named “_ActorDetailsPartial” and add the following code:

  1. @model List<BestMoviesModel.Actor>
  2. <ul class="responsive-MovieDetails">
  3. @if (Model.Count > 0)
  4. {
  5. foreach (var item in Model)
  6. {
  7. <li class="UserDetailList"><span class="UserDetailHeader">Name</span><span>@item.Name</span></li>
  8. <li class="UserDetailList"><span class="UserDetailHeader">Age</span><span>@item.Age</span></li>
  9. <li class="UserDetailList"><span class="UserDetailHeader">DOB</span><span>@item.DOB.ToShortDateString()</span></li>
  10. }
  11. }
  12. else
  13. {
  14. <li class="NoData">No data found</li>
  15. }
  16. </ul>

Step 3: Now change the “_MovieDetailsPartial.cshtml” page code with the highlighted code below:

  1. @model List<BestMoviesModel.MovieInfo>
  2. <ul class="responsive-MovieDetails">
  3. @if (Model.Count > 0)
  4. {
  5. foreach (var item in Model)
  6. {
  7. <li class="UserDetailList"><span class="UserDetailHeader">Director</span><span>@item.Director</span></li>
  8. <li class="UserDetailList"><span class="UserDetailHeader">IMDB Rating</span><span>@item.ImdbRating</span></li>
  9. <li class="UserDetailList"><span class="UserDetailHeader">Production</span><span>@item.Production</span></li>
  10. <li class="UserDetailList"><span class="UserDetailHeader">Filmfare Awards</span><span>@item.FilmfareAward</span></li>
  11. <li class="UserDetailList">
  12. <span class="UserDetailHeader">Lead Role</span><span id="MovieLeadRole">@item.LeadRole <a href="javascript:void(0)" onclick="GetActorDetails('@item.LeadRole')"><img src="~/Images/movie_Info.png" /></a></span>
  13. <div id="ActorContent"></div>
  14. </li>
  15. }
  16. }
  17. else
  18. {
  19. <li class="NoData">No data found</li>
  20. }
  21. </ul>
  22. <script>
  23. //This method is used to edit user location
  24. function GetActorDetails(name) {
  25. $.ajax({
  26. url: '@Url.Action("GetLeadRoleDetails", "Movie")',
  27. dataType: "html",
  28. data: { Name: name },
  29. type: "GET",
  30. error: function (xhr, status, error) {
  31. var err = eval("(" + xhr.responseText + ")");
  32. toastr.error(err.message);
  33. },
  34. success: function (data) {
  35. $("#MovieLeadRole").css("visibility", "hidden");
  36. $('#ActorContent').html("");
  37. $('#ActorContent').html(data);
  38. $('#ActorContent').show();
  39. }
  40. });
  41. }
  42. </script>

Step 4: Now run the application and click on the Movie Name to show the information,

application
Figure 21: Loading Partial View in Div Element

Step 5: Now click on the info icon and the newly added Partial View will load in the Div element as shown below:

Partial View in Div Element
Figure 22: Partial View in Div Element

That’s it.

Summary

So far this article describes how to show the data in the Partial View and how we can load Partial View in different manner as in jQuery UI Dialogue or in Div Element. Thanks for reading the article. Happy Coding!!

Read more articles on ASP.NET: