twitter-bootstrap Bootstrap Multiselect 获取 HttpPost 上的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29466959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:31:02  来源:igfitidea点击:

Bootstrap Multiselect Get Selected values on HttpPost

asp.net-mvctwitter-bootstrapasp.net-mvc-4

提问by user3035024

I'm using this Bootstrap Multiselectand my problem is that I cant get the selected values on HttpPost on ASP.Net MVC.

我正在使用这个Bootstrap Multiselect,我的问题是我无法在 ASP.Net MVC 上的 HttpPost 上获得选定的值。

Problems Encountered:

遇到的问题:

  • after clicking save, Only the first selected value is the present on the model. (SOLVED)

  • after clicking save, Only the first selected value is the present on the dropdownlist.

  • 单击保存后,只有第一个选定的值出现在模型上。(已解决

  • 单击保存后,只有第一个选定的值出现在下拉列表中。

Sample.chtml:

示例.chtml:

@model SampleProject.Models.SampleViewModel

@using (Html.BeginForm())
{
    @Html.DropDownListFor(model => model.Selected, new SelectList(Model.List, "value", "text"), new { @class = "multiselect form-control", multiple = "multiple" })
    <input type="submit" value="Save" />
}

Model:

模型:

public class SampleViewModel
{
    public string[] Selected { get; set; }
    public SelectList List { get; set; }
}

Controller:

控制器:

public class DashboardController : Controller
{
    public ActionResult Sample()
    {
        SampleViewModel model = new SampleViewModel();
        model.List = new SelectList(new List<string>() { "1", "2", "3" });

        return View(model);
    }

    [HttpPost]
    public ActionResult Sample(SampleViewModel model)
    {
        model.List = new SelectList(new List<string>() { "1", "2", "3" });

        return View(model);
    }
}

Selection:

选择:

Selection

选择

I cant get the selected values correctly on HttpPost

我无法在 HttpPost 上正确获取所选值

Code Behind/HttpPost: (Wrong) HttpPost

代码隐藏/HttpPost:(错误) 邮局

After HttpPost: (Correct)

HttpPost 之后:(正确)

After

后

回答by

A <select multiple="multiple">posts back an array of values (not a single value). You property Selectedneeds to be IEnumerable, for example

A<select multiple="multiple">回发一组值(不是单个值)。例如,您的财产Selected必须是IEnumerable

public string[] Selected { get; set; }

Side note: Since the text and value properties are the same, you can simplify you code by making the Listproperty SelectList

旁注:由于 text 和 value 属性相同,您可以通过使List属性简化代码SelectList

public SelectList List { get; set; }

and then in the controller

然后在控制器中

model.List = new SelectList(new List<string>() { "1", "2", "3" })

(although its not clear why you would not use int)

(虽然不清楚你为什么不使用int

回答by peterV

Didn't see this answer anywhere, so to solve the issue with having only 1 item selected after the post back you need to use:

在任何地方都没有看到此答案,因此要解决在回发后仅选择 1 个项目的问题,您需要使用:

@Html.ListBoxFor(.. 
instead of @Html.DropDownListFor