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("'", "'");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961809a4603433.html
评论列表(0条)