我有一个问题与我的Blazor应用程序,有两个输入和一个按钮
<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方法中,我使用"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>
}
Getdata
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;
愚蠢的错误,我所有的代码都在这样的标签:
<form>
all my code here
</form>
但是在@MrC aka Shaun Curtis回答没有这个表单标签,我从我的代码中删除它,现在一切工作如我所料,感谢帮助
这是目前为止你给我们展示的一个演示版本:
@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
后放置一个断点,并验证它返回您期望的列表。比较你的代码和我的代码,找出不同的地方,或者你没有给我们看的地方。有办法,不然就行了!
注意:当问题几乎总是在其他地方时,每个人都试图添加StateHasChanged
来解决渲染问题。