简体   繁体   English

尝试实现ListBoxFor时出现CS1973错误

[英]CS1973 error when trying to implement a ListBoxFor

I'm trying to implement a very simple multiple select in .NET where the drop-down values are dynamically created. 我正在尝试在.NET中实现一个非常简单的多项选择,在其中动态创建下拉值。

The Code: 编码:

I created a View Model: 我创建了一个视图模型:

public class FiltersViewModel
{
    public int[] SelectedNatures { get; set; }
    public IEnumerable<SelectListItem> Natures { get; set; }

    public void FillNatures()
    {
        var repository = new LeadRepository();
        IEnumerable<string> natures = repository.GetNatures();

        var items = new List<SelectListItem> {};
        foreach (var nature in natures)
        {

            items.Add(new SelectListItem {Value = (string) nature, Text = (string) nature});
            Natures = items.ToArray();

        }
    }
}

I created an instance of the model in the controller: 我在控制器中创建了模型的实例:

public ActionResult Map()
    {
        var model = new FiltersViewModel {SelectedNatures = new int[] {}};

        model.FillNatures();

        ViewData.Model = model; 

        return View();

    }

Then I added in the View: 然后我在视图中添加:

 <%: Html.ListBoxFor(x => x.SelectedValues, Model.Values) %>

Error: 错误:

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named "ListBoxFor" 编译器错误消息:CS1973:'System.Web.Mvc.HtmlHelper'没有名为“ ListBoxFor”的适用方法

Question: 题:

  • Is there a simpler way to implement a multiselect? 有没有更简单的方法来实现多重选择? (I just want to create it, then I use ajax to take the input) (我只想创建它,然后使用ajax接受输入)
  • What is the problem in my current code? 我当前的代码有什么问题?
  • How can I choose a class and id for my select? 如何为我选择的课程选择类别和ID?

It seems this question deals with the same error. 看来这个问题处理相同的错误。 I'll try to see if the answer works. 我将尝试看看答案是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试使用动态参数调用扩展方法时出现错误CS1973 - Why is there an error CS1973 when trying to invoke extension method with dynamic argument 尝试在MVC2中实现KENDO UI时出现编译错误:CS1705 - Compilation Error :CS1705 when trying to implement KENDO UI in MVC2 尝试在浏览器中查看 Service.cs 并使用此活动时出错 - Error when trying to view the Service.cs in browser and using this activity 尝试构建时出现 CS1003 和 CS1026 语法错误 - CS1003 and CS1026 Syntax error when I trying to build 尝试分配 class 时.Net 转换错误 CS0029 和 CS0266 - .Net Conversion error CS0029 and CS0266 when trying to assign class 在强类型列表上尝试“where”时出错,错误CS0103 - Error when trying to “where” on strongly typed list, error CS0103 尝试启用或禁用工具栏菜单项时出现错误CS0120 - Error CS0120 when trying to enable or disable a toolstrip menu item 编译器错误消息:CS1061尝试在表中显示上载的图像时 - Compiler Error Message: CS1061 when trying to display uploaded images in a table 尝试在 C# 中将强制转换运算符实现为可空类型时出错 - Error when trying to implement cast operator to nullable type in C# 尝试实现Fody加载项时发生错误 - Cast error when trying to implement Fody add-in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM