admin管理员组文章数量:1406919
I am creating a select list as follows:
<formselect id="CountryDropdown" class="form-select form-control form-control-sm"
asp-for="Country"
asp-items="@(new SelectList(@ViewBag.Countries,"TextColumn","TextColumn"))"></formselect>
@ViewBag.Countries contains the item
COTE D'IVOIRE
The formselect tag helper converts this to:
<option value="COTE D'IVOIRE">COTE D'IVOIRE</option>
How do I get the entry in the select list to be
<option value="COTE D'IVOIRE">COTE D'IVOIRE</option>
I am creating a select list as follows:
<formselect id="CountryDropdown" class="form-select form-control form-control-sm"
asp-for="Country"
asp-items="@(new SelectList(@ViewBag.Countries,"TextColumn","TextColumn"))"></formselect>
@ViewBag.Countries contains the item
COTE D'IVOIRE
The formselect tag helper converts this to:
<option value="COTE D'IVOIRE">COTE D'IVOIRE</option>
How do I get the entry in the select list to be
<option value="COTE D'IVOIRE">COTE D'IVOIRE</option>
Share
Improve this question
edited Mar 10 at 1:35
Zhi Lv
22k1 gold badge27 silver badges37 bronze badges
asked Mar 6 at 16:41
Edney HolderEdney Holder
1,2409 silver badges22 bronze badges
2 Answers
Reset to default 0Generally, in asp core Razor page, we are using the <select> tag helper. So, you can try to use it, refer to the following code:
In the Country.cshtml.cs file: before returning select options list to client or ViewBag, use HttpUtility.HtmlDecode()
method to decode the text and convert COTE D'IVOIRE
to COTE D'IVOIRE
.
public class CountryModel : PageModel
{
public string Country { get; set; }
public List<SelectListItem> Countries { get; set; }
public void OnGet()
{
Countries = new List<SelectListItem>
{
new SelectListItem { Value = HttpUtility.HtmlDecode("COTE D'IVOIRE"), Text = HttpUtility.HtmlDecode("COTE D'IVOIRE") },
new SelectListItem { Value = "Mexico", Text = "Mexico" },
new SelectListItem { Value = "Canada", Text = "Canada" },
new SelectListItem { Value = "USA", Text = "USA" },
new SelectListItem { Value = "COTE D'IVOIRE", Text = "COTE D'IVOIRE" },
};
}
}
Country.cshtml: Use the Html.Raw()
method to decode the text and use foreach statement to display the options
@page "/country"
@model Core8Razor.Pages.CountryModel
<select asp-for="Country" >
@foreach(var item in Model.Countries)
{
<option value="@item.Value">@Html.Raw(item.Text)</option>
}
</select>
The result as below:
About the <formselect>
, whether it is a custom tag helper (create by yourself) or a third-party component? If it is created by yourself, when display the value, you could use the above method to decode the value. If you are using third-party components, can you tell us which package you are using?
I solved this by editing the code that loaded ViewBag.Countries. The below code is used to eliminate D'
foreach (LookupListItem l in items)
l.TextColumn = l.TextColumn.Replace("'", "'");
本文标签: aspnet coreformselect url encoding of select list causing issuesStack Overflow
版权声明:本文标题:asp.net core - formselect url encoding of select list causing issues - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744961808a2634701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论