简体   繁体   中英

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. 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.

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" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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