C# MVC-如何获取列表框中的选定项';控制器中的s值?

C# MVC-如何获取列表框中的选定项';控制器中的s值?,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,我有一个应用程序,它有两个列表框: @using (Html.BeginForm()) { @Html.ListBoxFor(m => m.SelectedAttributes, Model.Attributes, new {id="listBoxAvail", SIZE = 5} ) <button type="button" id="add">MoveRight</button>

我有一个应用程序,它有两个列表框:

 @using (Html.BeginForm())
        {
           @Html.ListBoxFor(m => m.SelectedAttributes, Model.Attributes, new {id="listBoxAvail", SIZE = 5} ) 

            <button type="button" id="add">MoveRight</button>

            <button type="button" id="remove">"MoveLeft"></button>

            <button type="button" id="remove-all">RemAll</button>

            <button type="button" id="select-all">SelAll</button>

            <button type="button" id="up" onclick="MoveUp()">Up</button>

            <button type="button" id="down" onclick="MoveDown()">Down</button>

            @Html.ListBoxFor(m => m.SelectedAttributes2, Model.SelectedItems, new { id = "listBoxSel", SIZE = 5})

            <br />
            <p>Item Caption Text (140 Characters. HTML not allowed.)</p>
            @Html.TextAreaFor(m => m.ItemCaptionText)

            <input type="submit" id="submit"  value="Save"/>
        } 
在使用F12开发人员工具传输到右侧列表框后,我看到了文本和值。我想知道的是如何从控制器中的C#code中获取该值

[HttpPost]
        public ActionResult Index(OptInViewModel viewModel)
        {
            AttributeEntities db = new AttributeEntities();
            IEnumerable<string> items = viewModel.SelectedAttributes2;
            foreach (var item in items)
            {
                var temp = item;

                // Save it
                SelectedHarmonyAttribute attribute = new SelectedHarmonyAttribute();
                attribute.CustomLabel = viewModel.ItemCaptionText;
                //attribute.IsVisible = ?
                //attribute.OrderNumber = ?
                //attribute.HarmonyAttribute_ID

            }

            return View("Index");
        }
[HttpPost]
公共行动结果索引(OptInViewModel viewModel)
{
AttributeEntities db=新的AttributeEntities();
IEnumerable items=viewModel.selectedAttribute 2;
foreach(项目中的var项目)
{
var temp=项目;
//省省吧
SelectedHarmonyAttribute=新建SelectedHarmonyAttribute();
attribute.CustomLabel=viewModel.ItemCaptionText;
//attribute.IsVisible=?
//attribute.OrderNumber=?
//attribute.HarmonyAttribute\u ID
}
返回视图(“索引”);
}
ViewModel是:

public class OptInViewModel
    {
        public IEnumerable<string> SelectedAttributes { get; set; }
        public IEnumerable<string> SelectedAttributes2 { get; set; }
        public IEnumerable<SelectListItem> Attributes { get; set; }
        public IEnumerable<SelectListItem> SelectedItems { get; set; }
        public string ItemCaptionText { get; set; }
    }
公共类选项视图模型
{
公共IEnumerable SelectedAttribute{get;set;}
公共IEnumerable SelectedAttribute 2{get;set;}
公共IEnumerable属性{get;set;}
公共IEnumerable SelectedItems{get;set;}
公共字符串ItemCaptionText{get;set;}
}

我不完全理解您的问题,但据我所知,您试图在MVC中向函数调用传递一个参数。参数可以像在PHP中一样添加到URL

例如,函数可以定义为

void Function (string value);
调用它的URL是

~/Controller/Function?value=HelloWorld
您还可以设置路由。有关更多信息,请参阅有关StackOverflow的回答:

using (@Html.BeginForm("myMethod", "Controller", FormMethod.Post))

编辑:我看到您正在尝试使用表单提交

确保正确设置视图格式,不要忘记:

@model Path.To.Model
和控制器信息:

using (@Html.BeginForm("myMethod", "Controller", FormMethod.Post))

不,我没有向函数调用传递参数。我只是试图检索列表框的选定项的值,该值在post之后已经存在。对不起,我现在理解了-您正在尝试使用表单提交。改变了我的答案。这根本不是我要问的。我不是问如何使用我的post方法。它很快就到了。我在问如何从POST控制器代码获取列表框中所选项目的值。如果您正在加载另一个页面,请通过另一个模型将其传递到新页面并在那里使用。如果您在同一页上,则无法访问。请查看上面有关SelectedAttribute的评论2。这就是问题所在。由ListBoxFor填充。我在视图模型has列表中有它,但它只返回所选项目文本。我需要检索我在那里输入的值。不久前我必须这样做,创建一个隐藏字段,使其成为模型的一部分,并将其作为hiddenfor字段添加到mvc表单中。使您的单击函数添加/删除到新的隐藏字段,然后您的隐藏字段将具有值并作为模型的一部分传递。序列化程序在代码可读性方面也有很长的路要走(如果您的listbox特别处理w模型)。如果你选择这种方法,试一试&如果你遇到困难或有任何问题,请告诉我们。实际问题是什么。post方法中是否未正确填充
SelectedAttributes2
?另外,您的脚本可以简化为一行代码:
$(this.val('L'+$(this.val())。appendTo(“#listBoxSel”)
SelectedAttributes2已正确填充,但我将其作为一个列表,并提供带有属性值的字符串。我需要在项目中输入的值。