使用 MVC 2 中手动定义的 SelectListItems 中的 SelectList 进行呈现问题

发布于 2024-09-19 17:06:29 字数 1409 浏览 11 评论 0原文

我正在使用 ASP.NET MVC 2 (.NET 3.5),并且需要手动定义选项列表。当我这样做时,我会得到一个下拉菜单,其中每个手动条目都显示为“System.Web.Mvc.SelectListItem”。

我的视图模型将列表定义如下:

    public SelectList YesNoList
    {
      get
      {
        List<SelectListItem> tmpList = new List<SelectListItem>();
        tmpList.Add(new SelectListItem {Text = "", Value = ""});
        tmpList.Add(new SelectListItem {Text = "Yes", Value = "1"});
        tmpList.Add(new SelectListItem {Text = "No", Value = "0"});
        YesNoList = new SelectList(tmpList,"");
      }
      private set{}
     }

在视图中,我使用 Html.DropDownList 引用此列表:

Html.DropDownList("FieldName", viewmodel.YesNoList);

我期望在最终网页上呈现的内容应该是这样的:

<select id="FieldName" name="FieldName">
  <option value=""/>
  <option value="1">Yes</option>
  <option value="0">No</option>
</select>

相反,我得到:

<select id="FieldName" name="FieldName">
  <option>System.Web.Mvc.SelectListItem</option>
  <option>System.Web.Mvc.SelectListItem</option>
  <option>System.Web.Mvc.SelectListItem</option>
</select>

我不知所措,因为我无法弄清楚为什么返回类型,因此如果有人可以向我指出视图模型定义有什么问题,或者指出更好的方法,我将不胜感激。我犹豫是否要从 C# 类集合中派生 SelectList,因为 SelectList 将提供一种一致的方式来迭代值并显示文本。

预先感谢,希望有人可以提供帮助。

干杯,

J

I am using ASP.NET MVC 2 (.NET 3.5), and need to manually define what shall be an Options list. When I do so I get a drop down menu, with each of the manual entries reading 'System.Web.Mvc.SelectListItem'.

My view model defines the list as such:

    public SelectList YesNoList
    {
      get
      {
        List<SelectListItem> tmpList = new List<SelectListItem>();
        tmpList.Add(new SelectListItem {Text = "", Value = ""});
        tmpList.Add(new SelectListItem {Text = "Yes", Value = "1"});
        tmpList.Add(new SelectListItem {Text = "No", Value = "0"});
        YesNoList = new SelectList(tmpList,"");
      }
      private set{}
     }

In the view I reference this using the the Html.DropDownList:

Html.DropDownList("FieldName", viewmodel.YesNoList);

What I am expecting to be rendered on the final web page should be like:

<select id="FieldName" name="FieldName">
  <option value=""/>
  <option value="1">Yes</option>
  <option value="0">No</option>
</select>

Instead I get:

<select id="FieldName" name="FieldName">
  <option>System.Web.Mvc.SelectListItem</option>
  <option>System.Web.Mvc.SelectListItem</option>
  <option>System.Web.Mvc.SelectListItem</option>
</select>

I am at a loss, as I cannot figure out why the type is being returned so would appreciate it if anybody could point out to me what is wrong with the viewmodel definition, or point out a better way. I was hesitant to derive the SelectList from collections of C# classes as the SelectList would provide a consistant way to iterate through the values and display text.

Thanks in advance, hopefully somebody can help.

Cheers,

J

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在看孤独的风景 2024-09-26 17:06:29

下拉列表也可以处理 List,只需发送它即可。

Html.DropDownList("FieldName", viewmodel.YesNoList);

public List<SelectListItem> YesNoList
{
  get
  {
    List<SelectListItem> YesNoList = new List<SelectListItem>();
    YesNoList.Add(new SelectListItem {Text = "", Value = ""});
    YesNoList.Add(new SelectListItem {Text = "Yes", Value = "1"});
    YesNoList.Add(new SelectListItem {Text = "No", Value = "0"});
    return YesNoList;
  }
  private set{}
 }

你实际上在制作选择列表时做错了。

应该是:

new SelectList(tmpList, "Value", "Text"); 

然后忘记我上面的代码。如果您给它列表以及值和文本“key”,您可以对任何列表执行此操作

A dropdown can handle a List<SelectListItem> too, just send that in stead.

Html.DropDownList("FieldName", viewmodel.YesNoList);

and

public List<SelectListItem> YesNoList
{
  get
  {
    List<SelectListItem> YesNoList = new List<SelectListItem>();
    YesNoList.Add(new SelectListItem {Text = "", Value = ""});
    YesNoList.Add(new SelectListItem {Text = "Yes", Value = "1"});
    YesNoList.Add(new SelectListItem {Text = "No", Value = "0"});
    return YesNoList;
  }
  private set{}
 }

you are actually doing it wrong on making the selectlist.

it should be:

new SelectList(tmpList, "Value", "Text"); 

and then forget my above code. you can do this with any List, if you give it the list and the value and text "key"

清晨说晚安 2024-09-26 17:06:29

您可以使用编辑器模板来完成此操作。将其命名为“YesNo”并包含以下代码...

@Modeltype Boolean

@Code
  Dim YesNoList = New List(Of SelectListItem)()
  YesNoList.Add(New SelectListItem() With {.Text = "Yes", .Value = True})
  YesNoList.Add(New SelectListItem() With {.Text = "No", .Value = False})

  Dim list = New SelectList(YesNoList, "Value", "Text", Model)
End Code

@Html.DropDownList("", list)

然后在您的模型中为您的属性分配一个“YesNo”的 UIHint。这意味着现在 EditorFor 此属性将为您提供一个很好的是/否列表,该列表将绑定为布尔值。

You could do it by using an editor template. Call it 'YesNo' and include the following code...

@Modeltype Boolean

@Code
  Dim YesNoList = New List(Of SelectListItem)()
  YesNoList.Add(New SelectListItem() With {.Text = "Yes", .Value = True})
  YesNoList.Add(New SelectListItem() With {.Text = "No", .Value = False})

  Dim list = New SelectList(YesNoList, "Value", "Text", Model)
End Code

@Html.DropDownList("", list)

Then within your model assign a UIHint of 'YesNo' on your property. Which means that now the EditorFor this property will give you a nice Yes/No list that will bind as a boolean.

并安 2024-09-26 17:06:29

试试这个代码:

OdbcDataReader iLRt1 = databaseFunctions.databaseConnection.getFromDatabaseReader("select * from groups order by head");

List<SelectListItem> Hello1 = new List<SelectListItem>();
Hello1.Add(new SelectListItem { Text = "Select All", Value = "Select All" });

while (iLRt1.Read())
{
    Hello1.Add(new SelectListItem { Text = iLRt1["head"].ToString(), Value = iLRt1["code"].ToString() });}

ViewData["myList2"] = Hello1;

Try this code:

OdbcDataReader iLRt1 = databaseFunctions.databaseConnection.getFromDatabaseReader("select * from groups order by head");

List<SelectListItem> Hello1 = new List<SelectListItem>();
Hello1.Add(new SelectListItem { Text = "Select All", Value = "Select All" });

while (iLRt1.Read())
{
    Hello1.Add(new SelectListItem { Text = iLRt1["head"].ToString(), Value = iLRt1["code"].ToString() });}

ViewData["myList2"] = Hello1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文