.NET Core剃须刀页面上有一个SELECT列表,它似乎从未重置过:-
HTML:
<form asp-page="./TestDropdown" method="post">
<div class="col-12">
<strong>Gender in memory: @Model.Gender</strong>
</div>
<div class="row">
<div class="col-md-12">
<select asp-for="@Model.Gender" asp-items="@Model.GenderList" class="form-control">
<option value="">Gender</option>
</select>
</div>
</div>
<div class="row">
<div class="col-12">
<input asp-page-handler="Search" type="submit" value="Search" class="btn btn-default btn-block" />
<input asp-page-handler="ClearSearch" type="submit" value="Clear Search" class="btn btn-warning btn-block" />
</div>
</div>
</form>
C#:
public class TestDropdownModel : PageModel
{
public TestDropdownModel()
{
InitSearchList();
}
[BindProperty]
public string Gender { get; set; }
[BindProperty]
public SelectList GenderList { get; set; }
public async Task<ActionResult> OnPostClearSearch()
{
Gender = "";
InitSearchList();
return Page();
}
public async Task<ActionResult> OnPostSearch()
{
InitSearchList();
return Page();
}
private void InitSearchList()
{
GenderList = new SelectList(new List<string>() { "Male", "Female" })
}
public void OnGet()
{
}
}
预期行为
OnPostClearSearch()
,性别设置为“” 实际行为
OnPostClearSearch()
已执行,Gender
设置为“” <option selected="selected">Male</option>
答案 0 :(得分:1)
正在从ModelState重新填充该值。将ModelState.Clear();
添加到您的OnPostClearSearch
方法中:
public async Task<ActionResult> OnPostClearSearch()
{
Gender = "";
ModelState.Clear();
InitSearchList();
return Page();
}