Akash Patel

Akash Patel

  • NA
  • 117
  • 40.2k

Getting error on using partial view in asp.net core 2.2

Jan 22 2019 4:24 AM
I am working on a project. I am displaying a list of services (OurServices View) and when click on particular service a view (Single View) will be displayed in which other partial view (_Facial View) will be displayed with its particular model. But I am getting an error- NullReference Exception- Object reference not set to an instance of an object. Below is the code.
 
Our Services View code:
  1. @model IEnumerable<NiyaSpa.Models.Service>  
  2. @{  
  3. ViewData["Title"] = "OurServices";  
  4. Layout = "~/Views/Shared/_NiyaSpaLayout.cshtml";  
  5. }  
  6. <!-- breadcrumb -->  
  7. <div class="w3_breadcrumb">  
  8. <div class="breadcrumb-inner">  
  9. <ul>  
  10. <li><a href="index.html">Home</a> <i> /</i></li>  
  11. <li>Our Services</li>  
  12. </ul>  
  13. </div>  
  14. </div>  
  15. <!-- //breadcrumb -->  
  16. <!--/content-inner-section-->  
  17. <div class="w3_content_agilleinfo_inner">  
  18. <div class="container">  
  19. <div class="inner-agile-w3l-part-head">  
  20. <h2 class="w3l-inner-h-title">Our Services</h2>  
  21. </div>  
  22. <div>  
  23. <p style="text-align:center; font-size:medium;">We currently offer Facial, Waxing, and Body Massaging services to our clients. </p>  
  24. </div>  
  25. <br />  
  26. <div class="gallery-grids">  
  27. @foreach (var services in Model)  
  28. {  
  29. <div class="col-md-4 gallery-grid">  
  30. <div class="grid">  
  31. <figure class="effect-apollo">  
  32. <a class="example-image-link" asp-controller="OurServices" asp-action="SingleView" asp-route-serviceId="@Convert.ToInt32(services.Service_ID)" data-lightbox="example-set" >  
  33. <img src="~/images/g8.jpg" alt="" />  
  34. <figcaption>  
  35. <h3><p>@services.ServiceName</p></h3>  
  36. </figcaption>  
  37. </a>  
  38. </figure>  
  39. </div>  
  40. </div>  
  41. }  
  42. <div class="clearfix"> </div>  
  43. </div>  
  44. </div>  
  45. </div>  
  46. <!--//content-inner-section-->  
Our Services Controller:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. using NiyaSpa.Models;  
  7. namespace NiyaSpa.Controllers  
  8. {  
  9. public class OurServicesController : Controller  
  10. {  
  11. private NiyaDBContext _dbcontext=null ;  
  12. public OurServicesController(NiyaDBContext dbContext)  
  13. {  
  14. _dbcontext = dbContext;  
  15. }  
  16. public IActionResult OurServices()  
  17. {  
  18. return View(_dbcontext.Services.ToList());  
  19. }  
  20. public IActionResult SingleView(int ServiceId)  
  21. {  
  22. var serviceName = _dbcontext.Services.SingleOrDefault(s => s.Service_ID == ServiceId)?.ServiceName;  
  23. ViewBag.service_name = serviceName;  
  24. return View();  
  25. }  
  26. }  
  27. }  
Single View Code
  1. @model NiyaSpa.Models.Booking  
  2. @{  
  3. ViewData["Title"] = "SingleView";  
  4. Layout = "~/Views/Shared/_NiyaSpaLayout.cshtml";  
  5. }  
  6. @{  
  7. var sname = ViewBag.service_name;  
  8. }  
  9. <div class="w3_breadcrumb">  
  10. <div class="breadcrumb-inner">  
  11. <ul>  
  12. <li><a href="">Our Services</a> <i> /</i></li>  
  13. <li>@sname</li>  
  14. </ul>  
  15. </div>  
  16. </div>  
  17. <div class="w3_content_agilleinfo_inner">  
  18. <div class="container">  
  19. @{  
  20. await Html.PartialAsync("_Facials"new Booking());  
  21. }  
  22. </div>  
  23. </div>  
_Facial - Partial View Code
  1. @model NiyaSpa.Models.Booking  
  2. <div class="inner-agile-w3l-part-head">  
  3. <h2 class="w3l-inner-h-title">Facials</h2>  
  4. </div>  
  5. <div>  
  6. <p style="text-align:center; font-size:medium;">  
  7. We provide skin specific facials as listed below. Each contains 5-8 step treatments including cleansing, chemical or physical exfoliation, gel treatment, steam facial, toning and moisturizing. Our blood circulating facial massage allows products to absorb deep into the skin giving clients long lasting results.  
  8. </p>  
  9. </div>  
  10. <br />  
  11. <br />  
  12. <div class="row">  
  13. <div class="card-deck">  
  14. @foreach (var facialService in Model as IEnumerable<Booking>)  
  15. {  
  16. <div class="col-md-4">  
  17. <div class="card border-primary mb-3" style="width: 18rem;">  
  18. <img style="width:280px; height:180px; border-radius:10px; border-width:thin;" class="card-img-top" src="~/images/t2.jpg" alt="Card image cap">  
  19. <div class="card-body">  
  20. <h4 class="card-title" style="font-weight:600; margin-bottom:10px; margin-top:10px">@facialService.SubService.ServiceName </h4>  
  21. <p class="card-text" style="margin:0 0 10px; font-size:small;">  
  22. @facialService.SubService.Description.Substring(0, 100);  
  23. <a href="modal" data-toggle="modal" data-target="#ModalDescription">View More...</a>  
  24. </p>  
  25. <a asp-controller="OurServices" asp-action="BookAppointment" data-toggle="modal" data-target="#ModalBooking" class="btn btn-primary">Book Now</a>  
  26. </div>  
  27. </div>  
  28. </div>  
  29. <!-- Modal Description -->  
  30. <div class="modal fade" id="ModalDescription" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">  
  31. <div class="modal-dialog modal-dialog-centered" role="document">  
  32. <div class="modal-content">  
  33. <div class="modal-header">  
  34. <h5 class="modal-title" id="exampleModalLongTitle" style="font-weight:600; font-size:18px">@facialService.SubService.ServiceName </h5>  
  35. <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="font-size:22px; margin-top:-23px;">  
  36. <span aria-hidden="true">×</span>  
  37. </button>  
  38. </div>  
  39. <div class="modal-body" style="font-size:15px; padding:10px 15px; text-align:justify;">  
  40. <b>@facialService.SubService.Description.Split("", StringSplitOptions.None)</b> @facialService.SubService.Description  
  41. </div>  
  42. <div class="modal-footer">  
  43. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>  
  44. </div>  
  45. </div>  
  46. </div>  
  47. </div>  
  48. }  
  49. </div>  
  50. </div>  
  51. <div class="modal fade" id="ModalBooking" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">  
  52. <div class="modal-dialog modal-dialog-centered" role="document">  
  53. <div class="modal-content">  
  54. <div class="modal-header">  
  55. <h5 class="modal-title" id="exampleModalLongTitle" style="font-weight:600; color:crimson; font-family:cursive; font-size:28px; text-align:center;">Book an Appointment</h5>  
  56. <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="font-size:22px; margin-top:-28px;">  
  57. <span aria-hidden="true">×</span>  
  58. </button>  
  59. <div style="margin-top:10px;">  
  60. <p>  
  61. <label style="color:crimson; font-size:small;"><b>Booking Policies</b></label>  
  62. <ul style="font-size:11px; margin-top:2px;">  
  63. <li>  
  64. Please arrive 15 mins before your appointment.  
  65. </li>  
  66. <li>  
  67. For appointment cancellation, please call 24 hours before.  
  68. </li>  
  69. <li>  
  70. To make your experience more enjoyable please turn off your phone during body massages and facial.  
  71. </li>  
  72. </ul>  
  73. </p>  
  74. </div>  
  75. </div>  
  76. <div class="modal-body" style="font-size:15px; padding:15px 15px; text-align:justify; margin-top:-15px">  
  77. <div class="signup-form">  
  78. <form asp-controller="OurServices" asp-action="BookAppointment" method="post">  
  79. <div class="form-group">  
  80. <div class="input-group">  
  81. <span class="input-group-addon"><i class="fa fa-user"></i></span>  
  82. <input type="text" asp-for="Customer.CustomerName" placeholder="Customer Name" class="form-control" required="required" />  
  83. </div>  
  84. </div>  
  85. <div class="form-group">  
  86. <div class="input-group">  
  87. <span class="input-group-addon"><i class="fa fa-paper-plane"></i></span>  
  88. <input type="email" asp-for="Customer.Email_ID" placeholder="Email Address" class="form-control" required="required" />  
  89. </div>  
  90. </div>  
  91. <div class="form-group">  
  92. <div class="input-group">  
  93. <span class="input-group-addon"><i class="fa fa-phone"></i></span>  
  94. <input type="text" asp-for="Customer.PhoneNumber" placeholder="Contact Number" class="form-control" required="required" />  
  95. </div>  
  96. </div>  
  97. <div class="form-group">  
  98. <div class="input-group date">  
  99. <div class="input-group-addon">  
  100. <i class="fa fa-calendar"></i>  
  101. </div>  
  102. <input type="text" class="form-control pull-right" asp-for="BookingDate" placeholder="Appointment Date" required="required" id="datepicker">  
  103. </div>  
  104. <!-- /.input group -->  
  105. </div>  
  106. <div class="form-group">  
  107. <div class="input-group">  
  108. <span class="input-group-addon"><i class="fa fa-list"></i></span>  
  109. <input type="text" asp-for="SubService.ServiceName" class="form-control" placeholder="Service Name" disabled="disabled" />  
  110. </div>  
  111. </div>  
  112. <div class="form-group">  
  113. <div class="input-group">  
  114. <span class="input-group-addon"><i class="fa fa-dollar"></i></span>  
  115. <input type="text" asp-for="TotalAmount" class="form-control" placeholder="Total Amount" disabled="disabled" />  
  116. </div>  
  117. </div>  
  118. <br />  
  119. <div class="form-group">  
  120. <button type="submit" class="btn btn-primary btn-block btn-lg" style="background-color:crimson; border-color:crimson;">Book Appointment</button>  
  121. </div>  
  122. </form>  
  123. </div>  
  124. </div>  
  125. </div>  
  126. @*<div class="modal-footer">  
  127. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>  
  128. </div>*@  
  129. </div>  
  130. </div>  
I am invoking partial view using @await Html.PartialAysnc("viewname","model");

Answers (6)