简体   繁体   English

无法从MVC中的Post上的ListBoxFor和MultiSelectList获取表单数据

[英]Not getting form data from ListBoxFor and MultiSelectList on Post in MVC

I am struggling with getting data from the post of a form where I am sorting between two group. 我正在努力从要在两个小组之间进行排序的表格的帖子中获取数据。 The form looks like this. 该表格如下所示。 集团经理 As you can see, the get action is working great and the whole page works just fine.. Here's the .cshtml view 如您所见,get操作非常有效,整个页面都正常工作。这是.cshtml视图

@model InfoKeeper.Models.GroupManageVewModel
@{
    ViewBag.Title = "ManageGroup";
}

<h2>ManageGroup</h2>
@using (Html.BeginForm("ManageGroup","GroupManager", FormMethod.Post))
{
    <div class="nav-block">
        <div class="row">
            <div class="col-xs-3 col-xs-pull-1 col-xs-offset-2">
                <p>Available Groups</p>
                @Html.HiddenFor(m => m.GroupID)
                @Html.HiddenFor(m => m.CustomerID)
                @Html.ListBoxFor(m => m.ResultAvailable, Model.AvailableDepartmentsSL, new { @Name = "from[]", @id = "multiselect", @class = "form-control", @size = "8", @multiple = "multiple", @style = "min-width: 350px;margin-right:50px;" })
            </div>

            <div class="col-xs-2" style="padding-top:25px;">
                <button type="button" id="multiselect_rightAll" class="btn btn-block"><i class="glyphicon glyphicon-forward"></i></button>
                <button type="button" id="multiselect_rightSelected" class="btn btn-block"><i class="glyphicon glyphicon-chevron-right"></i></button>
                <button type="button" id="multiselect_leftSelected" class="btn btn-block"><i class="glyphicon glyphicon-chevron-left"></i></button>
                <button type="button" id="multiselect_leftAll" class="btn btn-block"><i class="glyphicon glyphicon-backward"></i></button>
                <input type="submit" value="save" class="btn btn-block btn-success">
            </div>

            <div class="col-xs-3">
                <p>Departments in Group</p>
                @Html.ListBoxFor(m => m.ResultInGroup, Model.DepartmentsInGroupSL, new { @Name = "to[]", @id = "multiselect_to", @class = "form-control", @size = "8", @multiple = "multiple", @style = "min-width: 350px" })
            </div>
        </div>
    </div>
}

Also, here is the controller... 另外,这是控制器...

public ActionResult ManageGroup(int GroupID, int CustomerID)
{
    GroupRepo repo = new GroupRepo();

    var model = repo.ManageGroup(GroupID, CustomerID);

    model.AvailableDepartmentsSL = new MultiSelectList(model.AvailableDepartments, "Key", "Value");
    model.DepartmentsInGroupSL = new MultiSelectList(model.DepartmentsInGroup, "Key", "Value");

    return View(model);
}

and the model... 和模型...

public class GroupManageVewModel
    {
        public MultiSelectList AvailableDepartmentsSL { get; set; }
        public MultiSelectList DepartmentsInGroupSL { get; set; }
        public Dictionary<int, string> DepartmentsInGroup { get; set; }
        public Dictionary<int, string> AvailableDepartments { get; set; }
        public int GroupID { get; set; }
        public int CustomerID { get; set; }
        public string GroupName { get; set; }
        public IEnumerable<int> ResultAvailable { get; set; }
        public IEnumerable<int> ResultInGroup { get; set; }
    }

But when I submit the form... This is what I get on Post... 但是当我提交表格时...这就是我在邮局上得到的...

WAT

So, the form is posting something ... the HiddenFors. 因此,表单正在发布某些内容 ... HiddenFors。 But for what ever reason my ResultAvailable and ResultInGroup are null... Please help! 但是出于什么原因,我的ResultAvailable和ResultInGroup为null ...请帮助!

You need to use List<int> for your ResultInGroup property. 您需要将List<int>用于ResultInGroup属性。 The modelbinder cannot bind to an IEnumerable , as it doesn't support indexing. modelbinder无法绑定到IEnumerable ,因为它不支持索引。

Okay, I am now getting the desired results. 好的,我现在得到预期的结果。 The answer lay in some thinking that Chris Pratt got me on... 答案在于克里斯·普拉特(Chris Pratt)吸引了我...

Okay! 好的! So, when the form submits the option elements in each of the options do get selected. 因此,当表单提交选项元素时,每个选项中的元素都会被选中。 The code that does that was in the source. 做到这一点的代码在源代码中。 It has to be something else... I think it has something to do with the name of the SelectList... in order to get the plug-in to work I had to change the name of the selectlists to "from[]" and "to[]" It has to have something to do with this.... when in MVC I believe that they are typically named the prop that they are being bound to... Will report back 它必须是其他东西...我认为这与SelectList的名称有关...为了使插件正常工作,我不得不将选择列表的名称更改为“ from []”和“ to []”与此相关。...在MVC中,我认为它们通常被命名为绑定对象的道具...将报告

and I was 100% correct (I never get to say that.) 我是100%正确的(我永远不会这么说。)

When I was putting this form together and getting the plug-in working I initially had the code from the documentation in my view which looked like... 当我将这种形式放在一起并使插件正常工作时,我最初看到的文档中的代码看起来像是...

     <select name="from[]" id="multiselect" class="form-control" size="8" multiple="multiple">
        <option value="1">Item 1</option>
        <option value="2">Item 5</option>
        <option value="2">Item 2</option>
        <option value="2">Item 4</option>
        <option value="3">Item 3</option>
    </select>

Instead of this I decided to use the HtmlHelpers that MVC gives you so that I could submit this form and have all the data exactly where I wanted and didn't have to build a WebApi Controller and capture all of the form data with jQuery... When doing this though the functionality of the plug-in wasn't working and it was because I was trying to use the HtmlHelper Html.ListBoxFor as such 取而代之的是,我决定使用MVC为您提供的HtmlHelpers,以便我可以提交此表单并将所有数据完全放在我想要的位置,而不必构建WebApi Controller并使用jQuery捕获所有表单数据。在执行此操作时,尽管插件的功能无法正常工作,这是因为我试图像这样使用HtmlHelper Html.ListBoxFor

@Html.ListBoxFor(m => m.ResultAvailable, Model.AvailableDepartmentsSL, new { @name = "from[]", @id = "multiselect", @class = "form-control", @size = "8", @multiple = "multiple", @style = "min-width: 350px;margin-right:50px;" })

But when I opened the page in dev tools the name attribute wasn't set to "from[]" it was set to "ResultAvailable" After some reading I discovered that I could get the name attribute set to "from[]" by changing 但是,当我在开发工具中打开页面时,名称属性未设置为“ from []”,而是将其设置为“ ResultAvailable”。经过一番阅读,我发现可以通过更改将名称属性设置为“ from []”

@name = "from[]"

to

@Name = "from[]"

Yeah, it was case sensitive! 是的,区分大小写! After this the control worked but then I ran into the issue that created this question. 在此之后,控件起作用了,但随后我遇到了创建此问题的问题。 The post was coming back with nulls... So after some thinking I thought man, I wonder what would happen if I just commented out the name attribute, meaning the control wouldn't work like in the past, and hit submit from there... To my surprise, it worked. 帖子返回空值...因此,经过一番思考,我以为是男人,我想知道如果我只注释掉name属性,那将发生什么,这意味着该控件将无法像过去那样工作,并从该位置提交。 ..让我惊讶的是,它有效。 I was right about MVC needing the name of the ListBox to not be changed since it was giving it the name it needed to do what I wanted it to do... So, naturally now the plug-in doesn't work, right? 我对MVC的要求是正确的,因为它要赋予ListBox所需的名称来执行我想做的事情……因此,自然而然地,该插件不起作用了,对吗? Wrong! 错误!

It works. 有用。 And I have no clue why... 而且我不知道为什么...

I should say that my solution does make me feel uneasy.. but it's a solution nonetheless. 我应该说我的解决方案确实让我感到不安..但这仍然是一个解决方案。 Thanks for the help SO. 感谢您的帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM