![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 真的有點難 ![]()
![]() |
https://www.youtube.com/watch?v=DHe4UghuQkU&t=1005s 程式碼都一模一樣 但新增與刪除功能不會實踐 資料庫 CREATE TABLE [dbo].[Employee]( [EmployeeId] [int] IDENTITY(1,1) NOT NULL, [Nane] [nvarchar](50) NULL, [Division] [nvarchar](50) NULL, CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ( [EmployeeId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO Employee.cs using System; using System.Collections.Generic; namespace BlazorApp1.Models { public partial class Employee { public int EmployeeId { get; set; } public string Nane { get; set; } public string Division { get; set; } } } TbkcContext.cs using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; namespace BlazorApp1.Models { public partial class TbkcContext : DbContext { public TbkcContext() { } public TbkcContext(DbContextOptions<TbkcContext> options) : base(options) { } public virtual DbSet<Employee> Employee { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Employee>(entity => { entity.Property(e => e.Division).HasMaxLength(50); entity.Property(e => e.Nane).HasMaxLength(50); }); OnModelCreatingPartial(modelBuilder); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); } } EmployeeService.cs using BlazorApp1.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace BlazorApp1.Data { public class EmployeeService { private readonly TbkcContext _context; public EmployeeService(TbkcContext context) { _context = context; } public List<Employee> GetEmployees() { return _context.Employee.ToList(); } public void AddEmployee(Employee insert) { _context.Employee.Add(insert); _context.SaveChanges(); } public void DelEmployee(Employee del) { _context.Employee.Remove(del); _context.SaveChanges(); } } } EmployeePage.razor @page "/employeePage" @using BlazorApp1.Data @using BlazorApp1.Models @inject EmployeeService EmployeeService <h1>Employee</h1> <div>姓名:<input id="Text1" type="text" @bind="addEmployee.Nane" /></div> <div>科室:<input id="Text1" type="text" @bind="addEmployee.Division" /></div> <div><button @onclick="AddEmployee">新增</button></div> <hr> @if (employeesdata == null) { <p><em>Loading...</em></p> } else { <table class="table"> <thead> <tr> <th>編號</th> <th>姓名</th> <th>科室</th> <th>編輯</th> </tr> </thead> <tbody> @foreach (var item in employeesdata) { <tr> <td>@item.EmployeeId</td> <td>@item.Nane</td> <td>@item.Division</td> <td><button @onclick="()=>DelEmployee(item)">刪除</button></td> </tr> } </tbody> </table> } @code { private List<Employee> employeesdata; public Employee addEmployee = new Employee() { Nane = "", Division = "" }; protected override void OnInitialized() { Getemployee(); } public void Getemployee() { employeesdata = EmployeeService.GetEmployees(); } public void AddEmployee() { EmployeeService.AddEmployee(addEmployee); addEmployee = new Employee() { Nane = "", Division = "" }; Getemployee(); } public void DelEmployee(Employee del) { EmployeeService.DelEmployee(del); Getemployee(); } } appsettings.json { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "TbkcDatabase": "Data Source=127.0.0.1;Initial Catalog=Tbkc;Persist Security Info=True;User ID=sa;Password=123456" } } 資料可以讀到 但新增跟刪除無法運作 ![]()
搜尋相關Tags的文章:
[ blazor ] ,
本篇文章發表於2020-08-28 11:00 |
1樓
作者回應
真的有點難 ![]() |
Startup.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using BlazorApp1.Data; using BlazorApp1.Models; using Microsoft.EntityFrameworkCore; namespace BlazorApp1 { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddDbContext<TbkcContext>(options => options.UseSqlServer(Configuration.GetConnectionString("TbkcDatabase"))); services.AddRazorPages(); services.AddServerSideBlazor(); services.AddSingleton<WeatherForecastService>(); //注入服務 EmployeeService services.AddScoped<EmployeeService>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); }); } } }
本篇文章回覆於2020-08-28 11:03
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
2樓
作者回應
真的有點難 ![]() |
電腦重開就好了 誤會一場
本篇文章回覆於2020-08-28 13:15
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
回覆 |
如要回應,請先登入. |