本文介绍了如何在 DropDownList 中保留空格 - ASP.net MVC Razor 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图中以下列方式绑定我的模型:

I'm binding my model in the following way in the view:

<%=Html.DropDownList("SelectedItem",new SelectList(Model.MyItems,"ItemId","ItemName")) %>

问题是我的项目文本是一个格式化文本,单词之间有空格,如下所示.

Issue is my item text is a formatted text with spaces in between words, as below.

#123  First          $234.00
#123  AnotherItem    $234.00
#123  Second         $234.00

即使在将空格添加到 DropDownList 之后,我也希望保留此项文本中的空格.但不幸的是,我的 DropDownList 显示它们没有空格,如下所示:

I want to retain the spaces in this item text even after they are added to DropDownList. But unfortunately my DropDownList shows them without spaces as below:

#123 First $234.00
#123 AnotherItem $234.00
#123 Second $234.00

当我查看页面的源代码时,这些空间是完整的,但在显示中却不是.我尝试添加 '&nbsp;' 而不是空格,但 SelectList(MVC 框架类)内部方法在将它们添加为下拉列表中的项目之前使用 HtmlEncode.

When I view the source of the page those spaces are intact but in display it is not. I've tried to add '&nbsp;' instead of spaces but SelectList (MVC framework class) internal method is using HtmlEncode before adding them as items in the dropdownlist.

有什么办法可以做到这一点吗?

Is there any way I can achieve this?

推荐答案

nbsp 在 html 中对应 "xA0" 作为 C# 字符串,所以用这个代替空格,当 HTML 编码时会产生 nbsp

nbsp in html corresponds to "xA0" as a C# string, so use this instead of spaces, when HTML encoded it will produce nbsp

这篇关于如何在 DropDownList 中保留空格 - ASP.net MVC Razor 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:44