asp.net core - formselect url encoding of select list causing issues - Stack Overflow

I am creating a select list as follows:<formselect id="CountryDropdown" class="form-

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&#x27;IVOIRE">COTE D&#x27;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&#x27;IVOIRE">COTE D&#x27;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
Add a comment  | 

2 Answers 2

Reset to default 0

Generally, 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&#x27;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&#x27;IVOIRE"), Text = HttpUtility.HtmlDecode("COTE D&#x27;IVOIRE") },
                new SelectListItem { Value = "Mexico", Text = "Mexico" },
                new SelectListItem { Value = "Canada", Text = "Canada" },
                new SelectListItem { Value = "USA", Text = "USA" },
                new SelectListItem { Value = "COTE D&#x27;IVOIRE", Text = "COTE D&#x27;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&#x27

foreach (LookupListItem l in items)
    l.TextColumn = l.TextColumn.Replace("'", "&apos;");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961809a4603433.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信