I am working with codefirst approach in blazor and mydatabase created successfully using code first approach
fist I create a project and then select a visual studio 2019 -> blazor app -> blazor server app
why my employee list page not render in blazor
I want to display emp table record in blazor but issue is not render
Emp.cs
namespace BlazorServerApp.Pages
{
public class Emp
{
public int empid { get; set; }
public string empname { get; set; }
public string empcountry { get; set; }
}
}
EmployeAccessLayer.css
namespace BlazorServerApp.DataAccess
{
public interface IEmployeAccessLayer
{
IEnumerable GetAllEmployees();
}
public class EmployeAccessLayer : IEmployeAccessLayer
{
private MyDbContext _context;
public EmployeAccessLayer(MyDbContext context)
{
_context = context;
}
public IEnumerable GetAllEmployees()
{
try
{
return _context.emps.ToList();
}
catch (Exception ex)
{
throw;
}
}
}
}
EmployeeController.css
namespace BlazorServerApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
IEmployeAccessLayer _employeAccessLayer;
public EmployeeController(IEmployeAccessLayer employeAccessLayer)
{
_employeAccessLayer = employeAccessLayer;
}
[HttpGet]
[Route("Index")]
public IEnumerable<Emp> Index()
{
return (IEnumerable<Emp>)_employeAccessLayer.GetAllEmployees();
}
}
}
if I am not writing httpclient then here give an error:
@*@inject HttpClient Http*@
@code {
Emp[] empList;
protected override async Task OnInitializedAsync()
{
empList = await Http.GetJsonAsync<Emp[]>("employee/index"); //the name http does not exist in the current context
}
}
As you are using server blazor, there is no need to make an http call to the same site. You would use http if you where writing a WASM blazor api, or calling a different server.
as you registered a named httpclient, you must fetch by name, you can not inject it. Instead you inject a http factory, and call the factory to get the http client. Read the services docs, and http client docs, don’t just copy code, or if you do, only copy
from one example.
also oninitalizedasync will fire twice, so you should check if the data call was already done.
note: server blazor was just a stopgap, you really should be using client blazor.
Member
16 Points
112 Posts
how to display list page in blazor
Aug 26, 2020 07:16 AM|rahulpas|LINK
I am working with codefirst approach in blazor and mydatabase created successfully using code first approach
fist I create a project and then select a visual studio 2019 -> blazor app -> blazor server app
why my employee list page not render in blazor
I want to display emp table record in blazor but issue is not render
Emp.cs
EmployeAccessLayer.css
EmployeeController.css
GetEmployee.razor
NavMenu.razor //here I want to add emloyee link when user click employee then display the database data
Startup.cs
but when I am run the project and write this line in address bar then give below error
see my database image I have 2 record in table I want to display them in blazor
see my blazor output
hierarchy of myproject
I want to display 2 record on that page in blazor?
help
All-Star
58444 Points
15765 Posts
Re: how to display list page in blazor
Aug 26, 2020 03:38 PM|bruce (sqlwork.com)|LINK
Because you registered a named client, a http factory is registered, not a client. Your inject should be a factory, see docs
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1
though it’s not clear why you are using an httpclient to call back to the same website. Why don’t you just inject a IEmployeAccessLayer
note: this is all razor page code, not blazor
Member
16 Points
112 Posts
Re: how to display list page in blazor
Aug 27, 2020 05:36 AM|rahulpas|LINK
if I am not writing httpclient then here give an error:
this is all razor page code, not blazor
what do your means I am newbie in blazor
All-Star
58444 Points
15765 Posts
Re: how to display list page in blazor
Aug 27, 2020 05:31 PM|bruce (sqlwork.com)|LINK
As you are using server blazor, there is no need to make an http call to the same site. You would use http if you where writing a WASM blazor api, or calling a different server.
as you registered a named httpclient, you must fetch by name, you can not inject it. Instead you inject a http factory, and call the factory to get the http client. Read the services docs, and http client docs, don’t just copy code, or if you do, only copy from one example.
also oninitalizedasync will fire twice, so you should check if the data call was already done.
note: server blazor was just a stopgap, you really should be using client blazor.
Member
16 Points
112 Posts
Re: how to display list page in blazor
Aug 28, 2020 10:16 AM|rahulpas|LINK
your means get method means(below code) call in blazor webassembly app(client side) project?
Member
16 Points
112 Posts
Re: how to display list page in blazor
Aug 28, 2020 10:51 AM|rahulpas|LINK
Hello bruce
thanks for your time
I create another project webassembly client side project
and then I create razor component
GetRecordData.razor
EmployeeController.cs
but give an error:
All-Star
58444 Points
15765 Posts
Re: how to display list page in blazor
Aug 28, 2020 03:14 PM|bruce (sqlwork.com)|LINK
The error means the website that blazor is calling threw an error. Use the browser network debugger to see the error.
Member
16 Points
112 Posts
Re: how to display list page in blazor
Aug 29, 2020 04:51 AM|rahulpas|LINK
Hello beuce
thanks for support
can you help more
see webapi response is 200 ok
when I am debugging I could not understood the error:
what is error?