0

型号代码……

    public class ConfigureModel
    {
        public bool Responsive { get; set; }

        public string Theams { get; set; }

    }

控制器

        ConfigureModel model = new ConfigureModel();

        model.Responsive = bootStrapCoreSettings.Responsive ;

        //model.Theams = bootStrapCoreSettings.Theams;

        var theams = "lsdjfldsk;lkdfjlsd;jldskfj;dlksfj;";

        model.Theams = them.Split(new char[] { ';' });

如何将theams值放入下拉列表...

需要指南或代码

4

4 回答 4

2

要在您的视图中使用 Html.Dropdownlist,您需要IEnumerable<SelectListItem> selectList在您的模型中有一个。所以你的模型会像这样改变:

public class ConfigureModel
{
    public bool Responsive { get; set; }
    public IEnumerable<string> TheamsList { get; set; }
}

您的控制器将具有:

    var theams = "lsdjfldsk;lkdfjlsd;jldskfj;dlksfj;";
    List<string> myList = theams.TrimEnd(';').Split(';').ToList();
    model.TheamsList = myList;

填充模型后,Razor 视图中的代码将如下所示:

@Html.DropDownListFor("Theams", Model.TheamsList)
于 2013-11-12T05:37:27.063 回答
1

型号代码

public class ConfigureModel
{
    public bool Responsive { get; set; }

    public SelectList TheamsList { get; set; }
    public string SelectedTheam { get; set; }
}

动作代码

ConfigureModel model = new ConfigureModel();
var theams = "lsdjfldsk;lkdfjlsd;jldskfj;dlksfj;";

// If you don't want a blank value for the bit after the last ';'
theams = theams.TrimEnd(';');

var theamsSplit = theams.Split(new char[] { ';' }).ToDictionary(s => s, s => s);
model.TheamsList = new SelectList(theams, "Key", "Value");

查看代码

@Html.DropDownListFor(m => m.SelectedTheam, Model.TheamsList)

Tangent:你说的是 Themes 而不是 Theams?

于 2013-11-12T05:11:32.500 回答
1

您的代码甚至无法编译:

// You try to assign an array to a string
model.Theams = them.Split(new char[] { ';' });

尝试将配置模型类更改为:

public class ConfigureModel
{
    public bool Responsive { get; set; }

    public List<string> Theams { get; set; }

}

以及您的调用代码类似于(示例以演示不是最佳实践);

var theams = "lsdjfldsk;lkdfjlsd;jldskfj;dlksfj;";
theams.Split(new char[] { ';' }).ToList().ForEach(theam => {
    model.Theams.Add(theam);
});

然后你有一个填充模型。在您的剃刀视图中使用它:

@Html.DropDownListFor( m => m.Theams, new SelectList(Model.Theams))
于 2013-11-12T05:12:23.317 回答
0

在您的代码中,

model.Theams 应该是数组类型。

将其转换为列表或数组后,只需将其传递给@HTML.EditorFor(model=> Theams) 它将是下拉列表。

于 2013-11-12T05:11:12.090 回答