在我正在构建的 blazor 应用程序中,我有以下 cshtml 代码,其中包含一个<input>供用户输入姓名的元素。我希望能够将输入元素的值绑定到 blazor 页面的 c# 函数部分中的 Name 属性。
我该怎么做呢?
我的代码如下:
@page "/nameUpload"
<p>
//This is the input element that I would like to bind
Enter your name: <input type="text" ><br />
</p>
@functions {
//This is the property that I want to bind the input to
private string Name { get; set; } = "";
}
Run Code Online (Sandbox Code Playgroud) 我更新了 Visual Studio 19,在此之前我的网站完全按照我的预期运行。现在,当我加载主页时出现错误,但在完成加载之前就中断了。我在下面提供了错误代码。我还想告诉您,我已经看过这篇文章(How to open CircuitOptions.DetailedErrors?)并尝试了所有方法,但没有成功。
Information: Normalizing '_blazor' to
'http://fakesite.com/_blazor'.
Error: There was an unhandled exception on the current circuit, so this
circuit will be terminated. For more details turn on detailed exceptions in
'CircuitOptions.DetailedErrors'
Information: Connection disconnected.
Run Code Online (Sandbox Code Playgroud)
实施汉克斯解决方案后出现新错误
Error: Microsoft.JSInterop.JSException: Could not find 'FormLayout' in 'window.DxBlazor'.
Error: Could not find 'FormLayout' in 'window.DxBlazor'.
at http://fakesite.com/_framework/blazor.server.js:8:27768
at Array.forEach (<anonymous>)
at d (http://fakesite.com/_framework/blazor.server.js:8:27729)
at http://fakesite.com/_framework/blazor.server.js:8:28342
at new Promise (<anonymous>)
at e.beginInvokeJSFromDotNet (http://fakesite.com/_framework/blazor.server.js:8:28316)
at http://fakesite.com/_framework/blazor.server.js:1:19148
at Array.forEach (<anonymous>) …Run Code Online (Sandbox Code Playgroud) 我开始使用 blazor,通过一个演示项目,我遇到了字符集问题,它不起作用,也许它没有在需要的地方实现,我真的不知道。\n所以我index.html在 wwwroot 内的 html 文件和我的组件中实现了字符集Index.razor。在我的Index.razor“我仅使用主体元素调用其他组件”中,需要重音的单词在哪里。
我在index.html 中的代码(我真的不知道这段代码的作用,它随演示一起提供):
\n\n<!DOCTYPE html>\n<html>\n<head>\n <meta charset="UTF-8">\n <meta http-equiv="X-UA-Compatible" content="ie=edge" />\n <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no" />\n <meta name="description" content="Guild Project"/>\n <title>Mission Control</title>\n <base href="/" />\n <script src="localStorage.js"></script>\n <link href="_content/RazorComponentsMaterialDesign/styles.css" rel="stylesheet" />\n <script src="_content/RazorComponentsMaterialDesign/script.js"></script>\n <script src="_content/RazorComponentsMaterialDesign/lib/material-components-web.js"></script>\n <link href="css/Layout.css" rel="stylesheet" />\n <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.js"></script>\n <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.css" rel="stylesheet" />\n</head>\n<body style="margin: 0px">\n\n <app>\n <!-- "Loading" spinner -->\n <div role="progressbar" class="mdc-linear-progress mdc-linear-progress--indeterminate">\n <div class="mdc-linear-progress__buffer"></div>\n <div class="mdc-linear-progress__bar mdc-linear-progress__primary-bar"><span class="mdc-linear-progress__bar-inner"></span></div>\n <div class="mdc-linear-progress__bar mdc-linear-progress__secondary-bar"><span class="mdc-linear-progress__bar-inner"></span></div>\n …Run Code Online (Sandbox Code Playgroud) 我正在实现一个自定义AuthenticationStateProvider并使用来自 mainLayout 中用户声明的信息。据我了解,执行NotifyAuthenticationStateChanged方法后应该自己重新渲染所有使用<AuthorizeView>etc的组件,但事实并非如此。此外,我为 mainLayout 实现了我自己的重新加载器,并StateHasChanged在用户登录后使用它重新加载它。但出于某种原因,它仍然认为没有人授权并在<NotAuthorized>块中呈现代码块。但是,如果我手动重新加载页面,GetAuthenticationStateAsync则执行方法并在<Authorized>呈现内部代码块之后。我做错了还是错误?我的CustomAuthenticationStateProvider代码:
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly ISessionStorageService _sessionStorage;
public CustomAuthenticationStateProvider(ISessionStorageService sessionStorage)
{
_sessionStorage = sessionStorage;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var userModel = await _sessionStorage.GetItemAsync<AuthorizedModel>("userModel");
var identity = new ClaimsIdentity();
if (userModel != null)
{
identity = new ClaimsIdentity( new []
{
//Some my claims
...
}, "api");
}
else
{
identity = new ClaimsIdentity(); …Run Code Online (Sandbox Code Playgroud) 我有一个页面add,它将向数据库添加一个项目OnValidSubmit,然后重定向到一个页面list。在页面上,list我想显示一条成功消息,如“添加项目”。
我只想显示一次成功消息“添加项目”。如果用户刷新页面,它将消失。因此,通过 传递的路由参数URL似乎是错误的,因为这将在刷新后继续存在。
那么,我如何一次性传递“显示成功消息”标志以及一些附加数据,例如项目名称?
提示:我正在使用 Blazor 服务器端
我正在尝试在 Blazor 组件中创建一条警报消息。我不知道该怎么做。我正在运行 ASP.NET Core 3.1 Blazor 服务器端。这是我尝试过的
组件功能:
private async Task ShowAlert()
{
await JSRuntime.InvokeAsync<object>("ShowMsg");
}
Run Code Online (Sandbox Code Playgroud)
Javascript互操作:
function ShowMsg() {
success = "Success!";
return success;
}
Run Code Online (Sandbox Code Playgroud)
文件host.cshtml:
<script src="~/BlazorInterop.js"></script>
Run Code Online (Sandbox Code Playgroud) 我正在使用 blazor 并且我正在应用代码优先方法
但问题是我的数据库没有创建
首先我创建一个项目,然后选择一个Visual Studio 2019 -> blazor app -> blazor server app
然后
我在 appsetting.json 添加 connetionstring
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=servername;Initial Catalog=databasename;User ID=userid;Password=password"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)
然后
上面的连接字符串名称在 startup.cs 中初始化
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个 Emp 类
Emp.cs
namespace BlazorServerApp.Pages
{
public class Emp:ComponentBase
{
public int empid { get; set; }
public …Run Code Online (Sandbox Code Playgroud) 使用services.AddSingleton<SomeService, SomeServiceImplementation>()而不是有services.AddSingleton<SomeServiceImplementation>()什么好处?
例如我有一个示例界面
interface ISampleInterface
{
void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
和一个样本类:
class SampleClass : ISampleInterface
{
public void DoSomething()
{
console.write("hi");
}
}
Run Code Online (Sandbox Code Playgroud)
不,我做 services.AddSingleton<SampleClass>()
为什么或何时使用services.AddSingleton<ISampleInterface, SampleClass>()?
谢谢你的帮助!:-)
启动了一个新的 Blazor WebAssembly 项目(Windows 10,Visual Studio 2019),尝试从服务器端通过 HttpClient 获取一个简单的列表。
错误:
00755c3a:0xa3a0 Uncaught (in promise) RuntimeError: memory access out of bounds
at malloc (<anonymous>:wasm-function[359]:0xa3a0)
at monoeg_malloc (<anonymous>:wasm-function[161]:0x3c71)
at stack_frag_new (<anonymous>:wasm-function[2019]:0x59758)
at add_frag (<anonymous>:wasm-function[1430]:0x3ccf2)
at interp_exec_method (<anonymous>:wasm-function[1120]:0x2f609)
at interp_runtime_invoke (<anonymous>:wasm-function[5655]:0xf7391)
at mono_jit_runtime_invoke (<anonymous>:wasm-function[5109]:0xddb3d)
at do_runtime_invoke (<anonymous>:wasm-function[1410]:0x3ba85)
at mono_runtime_try_invoke (<anonymous>:wasm-function[418]:0xcfdb)
at mono_runtime_invoke (<anonymous>:wasm-function[1610]:0x44b39)
Run Code Online (Sandbox Code Playgroud)
服务器端(正确返回):
[HttpGet]
public async Task<IEnumerable<UserDetailsRM>> Get()
{
var users = await UserService.GetUsers();
return users;
}
Run Code Online (Sandbox Code Playgroud)
客户端:尽管得到了结果,但不渲染任何东西。
<div class="userListWrapper">
@if (Users != null)
{
<table class="table table-borderless" style="font-size:12px; font-family:arial; height:
500px; …Run Code Online (Sandbox Code Playgroud) 当我单击按钮时,应调用 blazor 组件中的两个函数。尝试使用逗号、分号和括号。什么都行不通。
<div class="col-md-auto"><button type="button" class="btn btn-light btn-sm" @onclick="@SearchTable"@*call two methods SearchTable and SortTable*@>Search</button></div>
Run Code Online (Sandbox Code Playgroud)
下面给出两个函数
@functions{
public void SearchTable()
{
foreach (KeyValuePair<int, SearchCriteria> entry in SearchCritRefs)
{
entry.Value.setSearchCriteria(entry.Key);
}
}
public void SortTable()
{
foreach (KeyValuePair<int, SortCriteria> entry in SortCritRefs)
{
entry.Value.setSortCriteria(entry.Key);
}
}
}
Run Code Online (Sandbox Code Playgroud)