将 html select 绑定到长 C# 数组
Bind html select to array of long C#
我的问题是当我想在我的 DTO 中将 html select 绑定到 long[] 的 属性 时,它显示了一个带有 的下拉列表多重 selection 能力。但我希望它处于 non-multiple (single selection) 模式。
我的 DTO:
public class CreateSmsPattern
{
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[MaxLength(128, ErrorMessage = ValidationMessages.InvalidLength)]
public string Name { get; set; }
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[Range(1, long.MaxValue, ErrorMessage = ValidationMessages.IsRequired)]
public long SpecialListId { get; set; }
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[MaxLength(5000, ErrorMessage = ValidationMessages.InvalidLength)]
public string Message { get; set; }
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[Range(1, int.MaxValue, ErrorMessage = ValidationMessages.IsRequired)]
public int ParametersCount { get; set; }
public long[] ParametersList { get; set; }
}
和我的前端(请注意用户必须选择 3 次并且每次只能选择 1 个 selection,因为他的 selection 的顺序对我很重要):
<div class="col-md-4">
<div class="form-group">
<label class="control-label" asp-for="Command.ParametersList">Parameters List</label>
<select class="form-control" asp-for="Command.ParametersList" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Command.ParametersList" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Command.ParametersList" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Command.ParametersList" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Command.ParametersList" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Command.ParametersList" class="error"></span>
</div>
</div>
如果您需要三个选择,那么您应该为每个选择设置三个接收属性:
public long Selection1 { get; set; }
public long Selection2 { get; set; }
public long Selection3 { get; set; }
并使用您的标签助手定位这些属性:
<div class="form-group">
<select class="form-control" asp-for="Selection1" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Selection1" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Selection2" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Selection2" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Selection3" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Selection3" class="error"></span>
</div>
这样你也不必担心顺序。
我的问题是当我想在我的 DTO 中将 html select 绑定到 long[] 的 属性 时,它显示了一个带有 的下拉列表多重 selection 能力。但我希望它处于 non-multiple (single selection) 模式。 我的 DTO:
public class CreateSmsPattern
{
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[MaxLength(128, ErrorMessage = ValidationMessages.InvalidLength)]
public string Name { get; set; }
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[Range(1, long.MaxValue, ErrorMessage = ValidationMessages.IsRequired)]
public long SpecialListId { get; set; }
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[MaxLength(5000, ErrorMessage = ValidationMessages.InvalidLength)]
public string Message { get; set; }
[Required(ErrorMessage = ValidationMessages.IsRequired)]
[Range(1, int.MaxValue, ErrorMessage = ValidationMessages.IsRequired)]
public int ParametersCount { get; set; }
public long[] ParametersList { get; set; }
}
和我的前端(请注意用户必须选择 3 次并且每次只能选择 1 个 selection,因为他的 selection 的顺序对我很重要):
<div class="col-md-4">
<div class="form-group">
<label class="control-label" asp-for="Command.ParametersList">Parameters List</label>
<select class="form-control" asp-for="Command.ParametersList" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Command.ParametersList" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Command.ParametersList" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Command.ParametersList" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Command.ParametersList" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Command.ParametersList" class="error"></span>
</div>
</div>
如果您需要三个选择,那么您应该为每个选择设置三个接收属性:
public long Selection1 { get; set; }
public long Selection2 { get; set; }
public long Selection3 { get; set; }
并使用您的标签助手定位这些属性:
<div class="form-group">
<select class="form-control" asp-for="Selection1" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Selection1" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Selection2" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Selection2" class="error"></span>
</div>
<div class="form-group">
<select class="form-control" asp-for="Selection3" asp-items='new SelectList(Model.ParametersList, "Id", "Name")'>
<option value="-1">Choose one...</option>
</select>
<span asp-validation-for="Selection3" class="error"></span>
</div>
这样你也不必担心顺序。