Blazor语言 - 使用标记字符串创建的绑定选择字段



>我正在尝试绑定要通过标记字符串生成的选择字段的值。选择看起来正常,但值未按预期绑定。如何修复我的代码,以便选择字段可以绑定到变量 possibleValue?

<p>
@selectBarGeneration
Your selected value is @possibleValue
</p>
@code {
public string possibleValue;
private MarkupString selectBarGeneration = new MarkupString("<select @bind="possibleValue"><option>Default</option><option>Option 2</option></select>");
}

MarkupString 不用于逻辑,它用于呈现原始 HTML 字符串。尝试在原始 HTML 中放置逻辑是一个安全问题。

不要使用 MarkupString,而是使用 Razor 创建选择列表,甚至更好地使用 InputSelect 组件。

<select @bind="@selectedValue">
@foreach (var item in selectListItems)
{
<option value="@item">@item</option>
}
}
</select>
@code {
string selectedValue ="";
string[] selectListItemsv = { ... };
}
<InputSelect @bind-Value="model.SelectedItemString" class="form-control">
@foreach (var cnt in model.Countries)
{
<option value="@cnt.Id">@cnt.Name</option>
}
</InputSelect>

第三种选择是使用现成的组件,如下所示: https://telerik.com/blazor-ui

最新更新