使用asp.net mvc中的jsonresult将值绑定到多选

时间:2016-11-07 06:08:18

标签: jquery ajax asp.net-mvc razor jquery-multiselect

我有跟随ActionResult方法

    [HttpGet]
    public ActionResult AssignViewers(long? where)
    {
        try
        {
            if (where == null)
            {
                return RedirectToAction("Index", "Groups");
            }
            else
            {
                LoadGroups();
                ListViewers(where.ToString());

                return View();

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

ListViewers 是一个JsonResult方法,

    [HttpGet]
    public JsonResult Listviewers(string where)
    {
        try
        {
            var data = new AssignUserViewModel();
            if (where != null)
            {
                long? groupid = Convert.ToInt64(where);

                AssignUserViewModel objassignuser = ..
                ...

                data = objassignuser;
            }                

            return Json(data, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

所以我试图在 AssignViewers 视图初始加载时绑定此 AssignUserViewModel 模型属性,并在其下拉值更改时

@model Project.ViewModels.AssignUserViewModel
@{

}  

  <div class="col-lg-12">
       <form class="form-horizontal" role="form">
           <div class="form-group">
           <label class="col-md-1 control-label">Group Name</label>
           <div class="col-md-11">
           @Html.DropDownListFor(m => m.groupID, new SelectList(ViewBag.Groups, "value", "name"), "Select the group", new { @class = "form-control" , id = "groupvalue"})
           </div>
          </div>
          <div class="form-group">
          <label class="col-md-1 control-label">Viewers</label>
          <div class="col-md-11">
          <select name="country" class="multi-select" multiple="" id="assign_item">
              @if (Model != null)
              {
                  var viewers = Model.Viewerlist;
                  foreach (var viewers in assignedviewers)
                  {
                    <option value="@viewers .UserId" selected>@viewers .UserFName @viewers .UserLName</option>

                 }
             }
                  </select>
                  </div>
                </div>
            </form>

  <script type="text/javascript">
      $(document).ready(function () {
       multiselctpopulatefun();
      });

    $(function () {
        $('#groupvalue').change(function () {

            $("#assign_item option").remove();
            $("#assign_item").multiSelect('refresh');


            $.ajax({
                url: '@Url.Action("Listviewers", "Groups")',
                type: 'GET',
                data: { where: $('#groupvalue').val() },
                dataType: 'json',
                contentType: 'application/json',
                success: function (data)
                {
                    multiselctpopulatefun();
                }
            });
        });
    });

    function multiselctpopulatefun() {
        $('#assign_item').multiSelect({
            ...
        });

    }
</script>   

查看

enter image description here

尽管值在控制器中加载,但这不是视图中的模型属性值

0 个答案:

没有答案