I'm currently going through tutorials to help me learn Razor pages.
I've created a web app that displays information from SQL in the form of a table. What I'm trying to do now is when you click on the "Details" button next to the record, for it to show the details in a bootstrap model popup.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Cv2.Models;
using Cv2.Services;
using Microsoft.AspNetCore.Http;
namespace Cv2
{
public class AuthoriseDeviationModel : PageModel
{
private readonly IGet_WorkstationDeviationRequests get_WorkstationDeviationRequests;
public IEnumerable<DeviationRequests> CurrentDeviationRequests { get; set; }
public void OnGet()
{
CurrentDeviationRequests = get_WorkstationDeviationRequests.GetDeviationRequests("A13B", "5317DBA3");
}
public AuthoriseDeviationModel(IGet_WorkstationDeviationRequests get_WorkstationDeviationRequests)
{
this.get_WorkstationDeviationRequests = get_WorkstationDeviationRequests;
}
public void OnPostCompleteProcess(int WSQuestionID)
{
CurrentDeviationRequests = get_WorkstationDeviationRequests.GetDeviationRequests("A13B", "5317DBA3");
}
}
}
That's great thank you. So I I've added that in but It underlines with An object reference is required for the non static field, method, or property APiGet_WorkstationDeviationRequests.GetDeviationDetailAsyc.
This is my main page now AuthoriseDeviation.cshml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Cv2.Models;
using Cv2.Services;
using Microsoft.AspNetCore.Http;
namespace Cv2
{
public class AuthoriseDeviationModel : PageModel
{
private readonly IGet_WorkstationDeviationRequests get_WorkstationDeviationRequests;
public IEnumerable<DeviationRequests> CurrentDeviationRequests { get; set; }
public void OnGet()
{
CurrentDeviationRequests = get_WorkstationDeviationRequests.GetDeviationRequests("A13B", "5317DBA3");
}
public AuthoriseDeviationModel(IGet_WorkstationDeviationRequests get_WorkstationDeviationRequests)
{
this.get_WorkstationDeviationRequests = get_WorkstationDeviationRequests;
}
public void OnPostCompleteProcess(int WSQuestionID)
{
CurrentDeviationRequests = get_WorkstationDeviationRequests.GetDeviationRequests("A13B", "5317DBA3");
}
public async Task<PartialViewResult> OnGetDeviationDetailAsync(int id)
{
return Partial("_DeviationDetails", await APIGet_WorkstationDeviationRequests.GetDeviationDetailsAsync(id));
}
}
}
And the SQL connection class APIGet_WorkStationDeviationRequests.cs
using System;
using System.Collections.Generic;
using System.Text;
using Cv2.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
namespace Cv2.Services
{
public class APIGet_WorkstationDeviationRequests : IGet_WorkstationDeviationRequests
{
private readonly AppDbContext context;
public async Task<DeviationRequests> GetDeviationDetailsAsync(int id) => await context.apiDeviationRequests.Include(p => p.DeviationDesc).FirstOrDefaultAsync(p => p.DeviationLogID == id);
public APIGet_WorkstationDeviationRequests(AppDbContext context)
{
this.context = context;
}
public IEnumerable<DeviationRequests> GetDeviationRequests(string ModuleIdent, string SUB_ID)
{
var parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@SUB_ID", SUB_ID));
return context.apiDeviationRequests.FromSqlRaw<DeviationRequests>("dbo.Get_WorkstationDeviationRequests @SUB_ID", parameters: parameters.ToArray())
.ToList();
}
}
}
You forgot to include the service declaration and dependency injection in the master page. Plus you are not returning the partial. You are not following the blog which returns partial HTML used to populate the modal.
I though I recognized the handle to your blog :-) and thanks for your help.
So my main page now looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Cv2.Models;
using Cv2.Services;
using Microsoft.AspNetCore.Http;
namespace Cv2
{
public class AuthoriseDeviationModel : PageModel
{
private readonly IGet_WorkstationDeviationRequests get_WorkstationDeviationRequests;
public IEnumerable<DeviationRequests> CurrentDeviationRequests { get; set; }
public void OnGet()
{
CurrentDeviationRequests = get_WorkstationDeviationRequests.GetDeviationRequests("A13B", "5317DBA3");
}
public AuthoriseDeviationModel(IGet_WorkstationDeviationRequests get_WorkstationDeviationRequests)
{
this.get_WorkstationDeviationRequests = get_WorkstationDeviationRequests;
}
public void OnPostCompleteProcess(int WSQuestionID)
{
CurrentDeviationRequests = get_WorkstationDeviationRequests.GetDeviationRequests("A13B.1.18", "5317DBA3");
}
public async Task<PartialViewResult> OnGetDeviationDetailAsync(int id)
{
return Partial("_DeviationDetails", await get_WorkstationDeviationRequests.GetDeviationDetailsAsync(id));
}
}
}
It underlines GetDeviationDetailsAsync on the line:
With the message: IGet_WorkstationDeviationRequests does not contain a definition for GetDeviationDetailsAsync and no accessible extension method GetDeviationDetailsAsync accepting a first argument of type IGet_WorkstationDeviationRequests could not be found.
This is my IGet_WorkstationDeviationRequests.cs
using System;
using System.Collections.Generic;
using System.Text;
using Cv2.Models;
namespace Cv2.Services
{
public interface IGet_WorkstationDeviationRequests
{ IEnumerable<DeviationRequests> GetDeviationRequests(string ModuleIdent, string SUB_ID);
}
}
I'm guessing I need to write some code to retrieve this data now based on the ID?
using System;
using System.Collections.Generic;
using System.Text;
using Cv2.Models;
using System.Threading.Tasks;
namespace Cv2.Services
{
public interface IGet_WorkstationDeviationRequests
{ IEnumerable<DeviationRequests> GetDeviationRequests(string ModuleIdent, string SUB_ID);
public Task<DeviationRequests> GetDeviationDetailsAsync(int id);
}
}
The error has gone but I put a break point on this in AuthoiriseDeviation:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RazorSqlite.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
public PartialViewResult OnGetRolesListPartialAsync()
{
return Partial("_myPartial");
}
}
}
Did you change the JavaScript to include the handler name in the query string? If so, open the browser developer tools (F12) before you click the button and see what happens in the Network tab.
This is within my data layer called APIGet_WorkstationRequests.cs:
using System;
using System.Collections.Generic;
using System.Text;
using Cv2.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
namespace Cv2.Services
{
public class APIGet_WorkstationDeviationRequests : IGet_WorkstationDeviationRequests
{
private readonly AppDbContext context;
public async Task<DeviationRequests> GetDeviationDetailsAsync(int id) => await context.apiDeviationRequests.FirstOrDefaultAsync(p => p.DeviationLogID == id);
public APIGet_WorkstationDeviationRequests(AppDbContext context)
{
this.context = context;
}
public IEnumerable<DeviationRequests> GetDeviationRequests(string ModuleIdent, string SUB_ID)
{
var parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@SUB_ID", SUB_ID));
return context.apiDeviationRequests.FromSqlRaw<DeviationRequests>("dbo.Get_WorkstationDeviationRequests @SUB_ID", parameters: parameters.ToArray())
.ToList();
}
}
}
If I enter the url into a browser I get:
SqlException: Invalid object name 'apiDeviationRequests'.
This is my AppDBContext too:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Cv2.Models;
namespace Cv2.Services
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<ModuleDetails> ModuleDetails { get; set; }
public DbSet<DeviationRequests> apiDeviationRequests { get; set; }
}
}
Member
5 Points
8 Posts
How to pass data from table to model popup in razor pages
Feb 11, 2021 01:07 PM|codingitup|LINK
Hi Everyone,
I'm currently going through tutorials to help me learn Razor pages.
I've created a web app that displays information from SQL in the form of a table. What I'm trying to do now is when you click on the "Details" button next to the record, for it to show the details in a bootstrap model popup.
For this I've been following Mikes tutorial at: Razor Pages And Bootstrap - Modal Master Details (mikesdotnetting.com)
I've managed to get the model popup to open and pass the id but i'm struggling with the part of displaying the the details of the table.
In the tutorial it mentions about using the below code but not sure which part to put it in.
This is my code so far:
AuthoirseDeviation.cshtml
AuthoriseDeviation.cshtml.cs
_DeviationDetails.cshtml Partial View
Many thanks for your help on this.
All-Star
53101 Points
23659 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 01:19 PM|mgebhard|LINK
The code snippet goes in your master page. The master page is responsible for returning the partial content that populates the Bootstrap modal.
Member
5 Points
8 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 01:47 PM|codingitup|LINK
Hi Mgebhard,
That's great thank you. So I I've added that in but It underlines with An object reference is required for the non static field, method, or property APiGet_WorkstationDeviationRequests.GetDeviationDetailAsyc.
This is my main page now AuthoriseDeviation.cshml.cs:
And the SQL connection class APIGet_WorkStationDeviationRequests.cs
Many thanks for your help.
All-Star
53101 Points
23659 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 01:54 PM|mgebhard|LINK
You forgot to include the service declaration and dependency injection in the master page. Plus you are not returning the partial. You are not following the blog which returns partial HTML used to populate the modal.
All-Star
194524 Points
28081 Posts
Moderator
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 02:10 PM|Mikesdotnetting|LINK
That should be
And then in your view:
Member
5 Points
8 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 02:39 PM|codingitup|LINK
Hi Mike,
I though I recognized the handle to your blog :-) and thanks for your help.
So my main page now looks like this:
It underlines GetDeviationDetailsAsync on the line:
With the message: IGet_WorkstationDeviationRequests does not contain a definition for GetDeviationDetailsAsync and no accessible extension method GetDeviationDetailsAsync accepting a first argument of type IGet_WorkstationDeviationRequests could not be found.
This is my IGet_WorkstationDeviationRequests.cs
I'm guessing I need to write some code to retrieve this data now based on the ID?
All-Star
53101 Points
23659 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 03:23 PM|mgebhard|LINK
You are still missing the constructor injection bits. The GetDeviationDetailsAsync(id) does not match the interface signature.
Member
5 Points
8 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 03:32 PM|codingitup|LINK
Ah, that bit gets the initial data.
I've added this, not sure if it's correct:
The error has gone but I put a break point on this in AuthoiriseDeviation:
I was thinking this would break when I press one of the detail buttons but it doesn't.
All-Star
53101 Points
23659 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 04:00 PM|mgebhard|LINK
Basic example.
Partial
All-Star
194524 Points
28081 Posts
Moderator
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 04:12 PM|Mikesdotnetting|LINK
Did you change the JavaScript to include the handler name in the query string? If so, open the browser developer tools (F12) before you click the button and see what happens in the Network tab.
Member
5 Points
8 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 04:17 PM|codingitup|LINK
HI Mike,
Yes, I changed it to:
Ah with using F12 and looking at the network it ran into a 404 error. The URL produced was: https://localhost:44396/AuthoriseDeviation?handler=DeviationDetail&id=15
Ah that's because the handler is contained within a folder called quality so I've change it to:
It now breaks so I'll see what's happening now and come back to you. Thanks for the guidance.
Member
5 Points
8 Posts
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 05:21 PM|codingitup|LINK
So I've just looked at the F12 and Network tab and it now comes up with a 500 error. It looks like it's on this line:
This is within my data layer called APIGet_WorkstationRequests.cs:
If I enter the url into a browser I get:
This is my AppDBContext too:
All-Star
194524 Points
28081 Posts
Moderator
Re: How to pass data from table to model popup in razor pages
Feb 11, 2021 06:15 PM|Mikesdotnetting|LINK
The SQL expects a table or other object called apiDeviationRequests. Doesn't look like you have one with that name in the DB?