简体   繁体   English

数据未在 MVC 控制器中为 select2 列表框 Mvc 发布

[英]Data is not posting in MVC Controller for select2 List Box Mvc

I am using Multi-Select text box in my view.. Below is my code我在视图中使用多选文本框.. 下面是我的代码

View:看法:

@using (Ajax.BeginForm("Multiple", "Home",  new AjaxOptions { HttpMethod = "POST" }))
{
<div class="col-md-2  col-xs-2">Associated Segment</div>
    <div class="col-md-4  col-xs-4">
        @Html.ListBoxFor(model => model.SegmentList, new MultiSelectList(ViewBag.List, "Value", "Text", Model.SegmentList.AsEnumerable()), new { @class = "form-control tokenizationSelect2", @multiple = "multiple" })
    </div>

    <div class="col-md-12  col-xs-12">
        <div class="pull-right" style="padding:1%;">
            <button class="btn btn-sm btn-primary" id="btnSaveProbDiagnosis" type="submit" name="">save</button>
        </div>
    </div>

}
<link href="~/scripts/select2.min.css" rel="stylesheet" />
<script src="~/scripts/jquery-1.10.2.js"></script>
<script src="~/scripts/select2.js"></script>
<script src="~/scripts/select2.full.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".tokenizationSelect2").select2({
            placeholder: "Your favourite car", //placeholder
            tags: true,
            tokenSeparators: ['/', ',', ';', " "]
        });
    })
</script>

Controller:控制器:

        [HttpPost]
        public ActionResult Multiple(ViewModal viewmoddal)
        {                
            return View();
        }

Modal:模态:

public class ViewModal
    {
       public int QuestionId { get; set; }
       public string question { get; set; }
       public string type { get; set; }
       public List<SegmentList> SegmentList { get; set; }
       public List<Answer> option { get; set; }

    }

Its perfectly binding data in the UI.它在 UI 中完美绑定数据。 But when I am posting the data to controller data not going to controller.但是当我将数据发布到控制器时,数据不会发送到控制器。

someone please help me in this...有人请帮助我...

You need to define array of integers to bind to multi select and these will be posted to controller actions.您需要定义整数数组以绑定到多选,这些将被发布到控制器动作。 for.eg in your case define your model like this. for.eg 在你的情况下像这样定义你的模型。

public class ViewModal
{
  public int[] SelectedSegments { get; set; }
  public List<Segment> SegmentList { get; set; }
}

and in view并且鉴于

@Html.ListBoxFor(model => model.SelectedSegments, new MultiSelectList(ViewBag.List, "Value", "Text", Model.SegmentList.AsEnumerable()), new { @class = "form-control tokenizationSelect2", @multiple = "multiple" })

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

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