我有一些类似于以下的标记:
<select>
<option selected="selected">Apple</option>
<option selected="">Orange</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,"橙色"显示为所选项目.我原本期望将selected属性设为空白会撤消其效果.有没有办法写这个而不是简单地离开属性?
我有以下代码用于 InputSelect
<InputSelect class="form-control form-control form-control-sm"
placeholder="Role"
disabled="@IsReadOnly"
@bind-Value="Model.Role"
@onchange="@RoleChanged">
<option value="Member">Member</option>
<option value="Admin">Admin</option>
<option value="Pioner">Pioneer</option>
<option value="Retailer">Retailer</option>
</InputSelect>
Run Code Online (Sandbox Code Playgroud)
对于代码:
bool ShowCreated;
bool ShowUpdated;
bool IsReadOnly;
string SelectedRole;
public EditForm AccountForm;
public Multi.Dtos.RegistrationDto Model = new Dtos.RegistrationDto() { Role = "Member" };
public void RoleChanged(ChangeEventArgs e)
{
SelectedRole = e.Value.ToString();
Console.WriteLine(e.Value);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试选择一个新项目时,不会调用 RoleChange 函数。有人可以指出这有什么问题吗。正确的值已更改,但未调用事件。
所以我正在构建一个 Blazor 组件,我想在其中输入输入并触发 AJAX 请求以从服务器获取过滤的数据。我试过这个
<input type="text" @bind="NameFilter" @onchange="FilterChangedAsync" />
Run Code Online (Sandbox Code Playgroud)
但是这会导致错误
属性 'onchange' 用于此元素两次或更多次。属性必须是唯一的(不区分大小写)。'@bind' 指令属性使用了属性 'onchange'。
我想过在 NameFilter 属性设置器中调用该方法,但在这种情况下我不能等待它。实现所需行为的正确方法是什么?
我正在尝试基于Blazor在Blazor中进行Enable/Disable一组time输入checkbox;对于inputs类型button,以下解决方案有效,对于类型的输入time则无效:
有效的按钮输入解决方案
<button type="button"
class="@this.SetButton">
</button>
[Parameter] public bool state{get;set;}
public string SetButton() {
string result = state == true ? "" : "disabled";
return result;
}
Run Code Online (Sandbox Code Playgroud)
尝试输入无效的时间
<input bind="@IsDisabled" type="checkbox" />
<input class="@this.GetGroupState()" type="time" />
protected bool IsDisabled { get; set; }
public string GetGroupState() {
return this.IsDisabled ? "disabled" : "";
}
Run Code Online (Sandbox Code Playgroud)
PS在第一种情况下,bool它是另一个参数,component所以我不绑定它。但在第二种情况下,它必然会checkbox
模型:
public class FiltersModel
{
public CheckBoxListWithTitle Brand { get; set; }
}
public class CheckBoxListWithTitle
{
public List<FilterCheckBox> CheckBoxes { get; set; }
}
public class FilterCheckBox
{
public string Value { get; set; }
public bool Checked { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
剃刀:
@foreach (var item in Model.Brand.CheckBoxes)
{
<label>
@item.Value
<input type="checkbox" @onchange="@FilterChangedBrand" />
</label>
}
Run Code Online (Sandbox Code Playgroud)
@代码:
public FiltersModel Model { get; set; } // Initialized in OnParametersSet
private void FilterChangedBrand(UIChangeEventArgs e)
{
string newCheckedBrand = e.Value.ToString(); …Run Code Online (Sandbox Code Playgroud) 我想在绑定到模型值的 blazor 编辑表单中有一个 InputSelect,并且还有一个 onchange 事件,该事件根据新值更改模型中的其他属性。
绑定到 @bind-Value 和 @onchange 不起作用(我猜是因为绑定值同时使用输入的值和值更改属性。
我可以绑定到 oninput,但我想知道是否有更好的方法来做到这一点。
<InputSelect id="inputPeriod" name="inputPeriod" class="form-control" @bind-Value="model.Period" @oninput="periodChanged">
protected void periodChanged(ChangeEventArgs e)
{}
Run Code Online (Sandbox Code Playgroud)
我可以像这样绑定到 oninput
但理想情况下,我想在更新模型属性后绑定到 @onchange 事件,或者知道最佳实践是什么。如果不使用绑定值,模型验证将无法工作,所以我能想到的唯一替代方法是让更改事件在我的模型中的属性内工作,但这似乎是错误的