ASP.NET MVC多个选择下拉列表 [英] ASP.NET MVC multiple select dropdown

查看:136
本文介绍了ASP.NET MVC多个选择下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code,让用户选择窗体上的多个位置。

I am using the following code to let user select multiple locations on the form.

@Html.DropDownListFor(m => m.location_code, Model.location_type, new { @class = "form-control", @multiple = "multiple" }).

位置指定:code是列表< INT> 和LOCATION_TYPE是列表< SelectListItem> 填充了数据

location_code is an List<int> and location_type is List<SelectListItem> populated with data.

在code确实返回我的控制器所选择的值,但是当编辑按钮,用户点击通过不显示选定值,但不是对象显示什么也没有选择正常初始化的下拉列表。

The code does return me the selected values in the controller, but when the user clicks on edit button the object passed does not show the selected values but instead shows the normal initialized dropdown with nothing selected.

什么其实我是想的是,一旦用户提交表单(包括多重选择的值),它将转到如果详细信息,其中用户确认页面正确。如果不是他presses编辑按钮和对象又是传递给controller.In这个阶段就应该表现出的多重价值selected.Other领域表现正常。

What i actually want is that once the user submits the form (including the multiple selected values) it goes to a page where user confirms if the details are correct.If not he presses edit button and the object is again passed to controller.In this phase it should show the multiple values selected.Other fields behave properly .

对此有何见解?

推荐答案

在您的视图:

@Html.ListBoxFor(m => m.location_code, Model.location_type)

这就是你需要的。您正在使用的列表框控件,以便它已经一个多选择列表中。

That's all you need. You're using a ListBox control so it's already a multiple select list.

然后回到你的控制器就可以得到选择的项目是这样的:

Then back in your controller you can get the selected items like this:

[HttpPost]
public string SaveResults(List<int> location_code)
{

    if (location_code!= null)
    {
        return string.Join(",", location_code);
    }
    else
    {
        return "No values are selected";
    }
}

这篇关于ASP.NET MVC多个选择下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆