为什么我不能在 Blazor 中使用输入值?
Why i cannot use input values in Blazor?
我的 Blazor 应用有问题,有 2 个输入和 1 个按钮
<div class="form-group">
<label class="form-label">Фильм</label>
<input type="text" class="form-control" @bind="@filmname" placeholder="Введите название" required>
</div>
<div class="form-group">
<label>Год</label>
<input type="text" class="form-control" @bind="@year" placeholder="Введите год выхода фильма">
<small class="form-text text-muted">Это необязательное поле</small>
</div>
<div class="col-4">
<button class="btn btn-primary" id="sendbutton" type="submit" @onclick="GetData" disabled=@isLoading>Получить результат</button>
</div>
在 GetData 方法中,我使用 arg 调用另一个方法,例如“filmname + year”
@code {
private List<string>? answers = null;
private bool isLoading = false;
public string filmname = "";
public string year = "";
private int check = 1;
private async Task GetData()
{
check = 0;
isLoading = true;
string Text = "конец света";
@* if (year != "")
Text = filmname + " " + year;
else
Text = filmname;*@
answers = new List<string>();
check = 0;
answers = await Data.Exec.GetScoreAsync(Text);
isLoading = false;
check = 1;
StateHasChanged();
}
}
但是它没有出现在页面上,顺便说一句,如果它很重要,当我在 GetData 中定义字符串文本并且不触摸输入表单时一切正常。这里有什么问题?
尝试像这样显示“答案”的元素:
@if (check == 0)
{
<p>Loading [@answers?.Count] ...</p>
<div class="spinner-border" role="status">
<span class="sr-only">Loading...</span>
</div>
}
@if (answers == null || answers.Count == 0)
{
<p>no data</p>
}
else
{
<p>@answers[0]</p>
<p>@answers[1]</p>
<p>@answers[2]</p>
<p>@answers[3]</p>
}
获取数据
public static async Task<List<string>> GetScoreAsync(string FilmName)
{
List<string> alldata = new List<string>();
List<string> comments = await Program.ParserExec(FilmName, false);
List<string> scores = new List<string>();
List<string> comments_new = Norm(comments);
List<string> positive = new List<string>();
List<string> negative = new List<string>();
foreach (string comment in comments_new)
{
ModelInput input = new ModelInput()
{
Review = comment
};
ModelOutput result = ConsumeModel.Predict(input);
scores.Add(result.Prediction);
if (result.Prediction == "0")
negative.Add(result.Prediction);
else
positive.Add(result.Prediction);
}
double last_score = 0;
foreach (string score in scores)
last_score += Convert.ToInt32(score);
last_score /= scores.Count;
last_score *= 10;
alldata = new List<string>();
alldata.Add(Math.Round(last_score, 1).ToString());
alldata.Add(scores.Count.ToString());
alldata.Add(positive.Count.ToString());
alldata.Add(negative.Count.ToString());
Dictionary<string, double> scoresDictionary = ExecIvi.Exec(FilmName);
return alldata;
这是您目前向我们展示的演示工作版本:
@page "/"
<div class="form-group">
<label class="form-label">Фильм</label>
<input type="text" class="form-control" @bind="@filmname" placeholder="Введите название" required>
</div>
<div class="form-group">
<label>Год</label>
<input type="text" class="form-control" @bind="@year" placeholder="Введите год выхода фильма">
<small class="form-text text-muted">Это необязательное поле</small>
</div>
<div class="col-4">
<button class="btn btn-primary" id="sendbutton" type="submit" @onclick="GetData" disabled=@isLoading>Получить результат</button>
</div>
@if (check == 0)
{
<p>Loading [@answers?.Count] ...</p>
<div class="spinner-border" role="status">
<span class="sr-only">Loading...</span>
</div>
}
@if (answers == null || answers.Count == 0)
{
<p>no data</p>
}
else
{
<p>@answers[0]</p>
<p>@answers[1]</p>
<p>@answers[2]</p>
<p>@answers[3]</p>
}
@code {
private List<string>? answers = null;
private bool isLoading = false;
public string filmname = "";
public string year = "";
private int check = 1;
private async Task GetData()
{
check = 0;
isLoading = true;
string Text = "конец света";
@* if (year != "")
Text = filmname + " " + year;
else
Text = filmname;*@
answers = new List<string>();
check = 0;
answers = await GetAnswersAsync();
isLoading = false;
check = 1;
// not needed;
// StateHasChanged();
}
private async Task<List<string>> GetAnswersAsync()
{
await Task.Delay(1000);
return new List<string> { "France", "Portugal", "Spain", "Australia" };
}
}
如果您 运行 这段代码,它会做它应该做的事情。我用你的 GetScoreAsync
代替了一个简单的异步模拟器,但它做同样的事情。
所以:
在您调用 GetScoreAsync
之后放置一个断点并验证它 returns 您期望的列表。
将您的代码与我的代码进行比较,找出不同之处,或者您没有向我们展示的内容。有东西或者它会起作用!
注意:当问题几乎总是出在别处时,每个人都试图添加一个 StateHasChanged
来解决渲染问题。
愚蠢的错误,我所有的代码都在这样的标签中:
<form>
all my code here
</form>
但是在@MrC aka Shaun Curtis 的回答中没有这个表单标签,我从我的代码中删除了它,现在一切正常,谢谢你的帮助
我的 Blazor 应用有问题,有 2 个输入和 1 个按钮
<div class="form-group">
<label class="form-label">Фильм</label>
<input type="text" class="form-control" @bind="@filmname" placeholder="Введите название" required>
</div>
<div class="form-group">
<label>Год</label>
<input type="text" class="form-control" @bind="@year" placeholder="Введите год выхода фильма">
<small class="form-text text-muted">Это необязательное поле</small>
</div>
<div class="col-4">
<button class="btn btn-primary" id="sendbutton" type="submit" @onclick="GetData" disabled=@isLoading>Получить результат</button>
</div>
在 GetData 方法中,我使用 arg 调用另一个方法,例如“filmname + year”
@code {
private List<string>? answers = null;
private bool isLoading = false;
public string filmname = "";
public string year = "";
private int check = 1;
private async Task GetData()
{
check = 0;
isLoading = true;
string Text = "конец света";
@* if (year != "")
Text = filmname + " " + year;
else
Text = filmname;*@
answers = new List<string>();
check = 0;
answers = await Data.Exec.GetScoreAsync(Text);
isLoading = false;
check = 1;
StateHasChanged();
}
}
但是它没有出现在页面上,顺便说一句,如果它很重要,当我在 GetData 中定义字符串文本并且不触摸输入表单时一切正常。这里有什么问题?
尝试像这样显示“答案”的元素:
@if (check == 0)
{
<p>Loading [@answers?.Count] ...</p>
<div class="spinner-border" role="status">
<span class="sr-only">Loading...</span>
</div>
}
@if (answers == null || answers.Count == 0)
{
<p>no data</p>
}
else
{
<p>@answers[0]</p>
<p>@answers[1]</p>
<p>@answers[2]</p>
<p>@answers[3]</p>
}
获取数据
public static async Task<List<string>> GetScoreAsync(string FilmName)
{
List<string> alldata = new List<string>();
List<string> comments = await Program.ParserExec(FilmName, false);
List<string> scores = new List<string>();
List<string> comments_new = Norm(comments);
List<string> positive = new List<string>();
List<string> negative = new List<string>();
foreach (string comment in comments_new)
{
ModelInput input = new ModelInput()
{
Review = comment
};
ModelOutput result = ConsumeModel.Predict(input);
scores.Add(result.Prediction);
if (result.Prediction == "0")
negative.Add(result.Prediction);
else
positive.Add(result.Prediction);
}
double last_score = 0;
foreach (string score in scores)
last_score += Convert.ToInt32(score);
last_score /= scores.Count;
last_score *= 10;
alldata = new List<string>();
alldata.Add(Math.Round(last_score, 1).ToString());
alldata.Add(scores.Count.ToString());
alldata.Add(positive.Count.ToString());
alldata.Add(negative.Count.ToString());
Dictionary<string, double> scoresDictionary = ExecIvi.Exec(FilmName);
return alldata;
这是您目前向我们展示的演示工作版本:
@page "/"
<div class="form-group">
<label class="form-label">Фильм</label>
<input type="text" class="form-control" @bind="@filmname" placeholder="Введите название" required>
</div>
<div class="form-group">
<label>Год</label>
<input type="text" class="form-control" @bind="@year" placeholder="Введите год выхода фильма">
<small class="form-text text-muted">Это необязательное поле</small>
</div>
<div class="col-4">
<button class="btn btn-primary" id="sendbutton" type="submit" @onclick="GetData" disabled=@isLoading>Получить результат</button>
</div>
@if (check == 0)
{
<p>Loading [@answers?.Count] ...</p>
<div class="spinner-border" role="status">
<span class="sr-only">Loading...</span>
</div>
}
@if (answers == null || answers.Count == 0)
{
<p>no data</p>
}
else
{
<p>@answers[0]</p>
<p>@answers[1]</p>
<p>@answers[2]</p>
<p>@answers[3]</p>
}
@code {
private List<string>? answers = null;
private bool isLoading = false;
public string filmname = "";
public string year = "";
private int check = 1;
private async Task GetData()
{
check = 0;
isLoading = true;
string Text = "конец света";
@* if (year != "")
Text = filmname + " " + year;
else
Text = filmname;*@
answers = new List<string>();
check = 0;
answers = await GetAnswersAsync();
isLoading = false;
check = 1;
// not needed;
// StateHasChanged();
}
private async Task<List<string>> GetAnswersAsync()
{
await Task.Delay(1000);
return new List<string> { "France", "Portugal", "Spain", "Australia" };
}
}
如果您 运行 这段代码,它会做它应该做的事情。我用你的 GetScoreAsync
代替了一个简单的异步模拟器,但它做同样的事情。
所以:
在您调用
GetScoreAsync
之后放置一个断点并验证它 returns 您期望的列表。将您的代码与我的代码进行比较,找出不同之处,或者您没有向我们展示的内容。有东西或者它会起作用!
注意:当问题几乎总是出在别处时,每个人都试图添加一个 StateHasChanged
来解决渲染问题。
愚蠢的错误,我所有的代码都在这样的标签中:
<form>
all my code here
</form>
但是在@MrC aka Shaun Curtis 的回答中没有这个表单标签,我从我的代码中删除了它,现在一切正常,谢谢你的帮助