【问题标题】:ASP.NET MVC 2 Editor TemplatesASP.NET MVC 2 编辑器模板
【发布时间】:2010-09-30 22:38:22
【问题描述】:

以下链接解释了编辑器模板:http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx

我想知道的是如果我有一个下拉的编辑器模板,初始值是如何设置的?

我有一个下拉菜单,我使用 Html.EditorFor(c => c.Country, "CountryDropDown")

但它始终默认为列表中的第一个选定项目...有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    我认为,您需要创建一个视图数据,或创建一个视图模型以包含传递给下拉列表的选择列表。例如,在您的控制器操作中,您应该这样做:

        //get your item for editing here i.e named itemToEdit
        //get your country collection here
    
          ArrayList countryList=New ArrayList;
           foreach (Country c In YourCountryCollection)
    {          countryList.Add(New With {.Item = c.CountryName, .value = c.CountryID})
    }
        Viewdata("CountryList")=New SelectList(countryList, "Value", "Item", itemToEdit.countryID)}
    

    现在在您看来,您应该使用以下内容,而不是使用 html.editorfor: Html.Editor("CountryLis", "CountryDropDown")

    这应该使用所选值设置您的下拉列表。 希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多