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:
- @model IEnumerable<NiyaSpa.Models.Service>
- @{
- ViewData["Title"] = "OurServices";
- Layout = "~/Views/Shared/_NiyaSpaLayout.cshtml";
- }
- <!-- breadcrumb -->
- <div class="w3_breadcrumb">
- <div class="breadcrumb-inner">
- <ul>
- <li><a href="index.html">Home</a> <i> /</i></li>
- <li>Our Services</li>
- </ul>
- </div>
- </div>
- <!--
- <!--/content-inner-section-->
- <div class="w3_content_agilleinfo_inner">
- <div class="container">
- <div class="inner-agile-w3l-part-head">
- <h2 class="w3l-inner-h-title">Our Services</h2>
- </div>
- <div>
- <p style="text-align:center; font-size:medium;">We currently offer Facial, Waxing, and Body Massaging services to our clients. </p>
- </div>
- <br />
- <div class="gallery-grids">
- @foreach (var services in Model)
- {
- <div class="col-md-4 gallery-grid">
- <div class="grid">
- <figure class="effect-apollo">
- <a class="example-image-link" asp-controller="OurServices" asp-action="SingleView" asp-route-serviceId="@Convert.ToInt32(services.Service_ID)" data-lightbox="example-set" >
- <img src="~/images/g8.jpg" alt="" />
- <figcaption>
- <h3><p>@services.ServiceName</p></h3>
- </figcaption>
- </a>
- </figure>
- </div>
- </div>
- }
- <div class="clearfix"> </div>
- </div>
- </div>
- </div>
- <!--
Our Services Controller:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using NiyaSpa.Models;
- namespace NiyaSpa.Controllers
- {
- public class OurServicesController : Controller
- {
- private NiyaDBContext _dbcontext=null ;
- public OurServicesController(NiyaDBContext dbContext)
- {
- _dbcontext = dbContext;
- }
- public IActionResult OurServices()
- {
- return View(_dbcontext.Services.ToList());
- }
- public IActionResult SingleView(int ServiceId)
- {
- var serviceName = _dbcontext.Services.SingleOrDefault(s => s.Service_ID == ServiceId)?.ServiceName;
- ViewBag.service_name = serviceName;
- return View();
- }
- }
- }
Single View Code
- @model NiyaSpa.Models.Booking
- @{
- ViewData["Title"] = "SingleView";
- Layout = "~/Views/Shared/_NiyaSpaLayout.cshtml";
- }
- @{
- var sname = ViewBag.service_name;
- }
- <div class="w3_breadcrumb">
- <div class="breadcrumb-inner">
- <ul>
- <li><a href="">Our Services</a> <i> /</i></li>
- <li>@sname</li>
- </ul>
- </div>
- </div>
- <div class="w3_content_agilleinfo_inner">
- <div class="container">
- @{
- await Html.PartialAsync("_Facials", new Booking());
- }
- </div>
- </div>
_Facial - Partial View Code
I am invoking partial view using @await Html.PartialAysnc("viewname","model");