【问题标题】:Multi Select dropdownlistfor not populating for jquery chosenMulti Select dropdownlistfor not populating jquery selected
【发布时间】:2014-09-10 06:00:31
【问题描述】:

我正在使用选择的 jquery,并且在多选的情况下我面临填充下拉列表的问题。当我的控制器返回到名为“CreateService”的视图时,下拉列表不会填充任何内容,只是空白。

查看

@{
    List<SelectListItem> listofControls = new List<SelectListItem>();
    listofControls.Add(new SelectListItem
    {
     Text = "Contact Selector",
     Value = "1"
    });
   listofControls.Add(new SelectListItem
   {
    Text = "Value Selector",
    Value = "2"
   });
   listofControls.Add(new SelectListItem
   {
    Text = "Text Box",
    Value = "3"
   });    
}

<div>

@Html.DropDownListFor(m => m.Mechanism.Controls, listofControls, new { @class = "chosen-select", @style = "width:350px", multiple = "multiple" })
</div>


<script>

// A $( document ).ready() block.
$(document).ready(function () {
    $(".chosen-select").chosen();
});
</script>

控制器

    public ActionResult EditService(int Id)
    {
        WCMSDataContext wcmsContext = new WCMSDataContext();
        ServiceVM serviceVM = new ServiceVM();
        serviceVM.Mode = new Mode();
        serviceVM.Mechanism = new Mechanism();

        var xService = from p in wcmsContext.Services where p.Id == Id select p;

        if (xService.Count() > 0)
        {
            XmlDocument xdoc = new XmlDocument();
            XmlNodeList xmlnode;
            // if xml coming via string
            string myXml = xService.First().XML;
            xdoc.LoadXml(myXml);
            //XmlNodeList address = xdoc.GetElementsByTagName("Service");
            xmlnode = xdoc.GetElementsByTagName("Service");

            xmlnode[0].ChildNodes.Item(0).InnerText.Trim(); //get all child nodes

            for (int j = 0; j < xmlnode[0].ChildNodes.Count; j++)
            {
                string nodeTitle = xmlnode[0].ChildNodes.Item(j).Name.Trim();
                string nodeValuestr = xmlnode[0].ChildNodes.Item(j).InnerText.Trim();

                if (nodeTitle == "TITLE")
                {
                    serviceVM.Title = nodeValuestr;
                }
                else if (nodeTitle == "DETAIL")
                {
                    serviceVM.Detail = nodeValuestr;
                }
                else if (nodeTitle == "EQUIPID")
                {
                    serviceVM.EquipId = nodeValuestr;

                    if (nodeValuestr == "0")
                    {
                        serviceVM.ServiceType = "One Shot";
                    }
                    else
                    {
                        serviceVM.ServiceType = "Subscription";
                    }
                }
                else if (nodeTitle == "MODE")
                {
                    serviceVM.Mode.ModeType = nodeValuestr;
                }
                else if (nodeTitle == "SMSCOMMAND")
                {
                    serviceVM.Mode.SMSCommand = nodeValuestr;
                }
                else if (nodeTitle == "DEACTIVATIONCOMMAND")
                {
                    serviceVM.Mode.DeactivationCommand = nodeValuestr;
                }
                else if (nodeTitle == "DIALCOMMAND")
                {
                    serviceVM.Mode.DialCommand = nodeValuestr;
                }
                else if (nodeTitle == "Mechanism")
                {
                    for (int k = 0; k < xmlnode[0].ChildNodes[j].ChildNodes.Count; k++)
                    {
                        nodeTitle = xmlnode[0].ChildNodes[j].ChildNodes.Item(k).Name.Trim();
                        nodeValuestr = xmlnode[0].ChildNodes[j].ChildNodes.Item(k).InnerText.Trim();

                        if (nodeTitle == "Title")
                        {
                            serviceVM.Mechanism.Title = nodeValuestr;
                        }
                        else if (nodeTitle == "Description")
                        {
                            serviceVM.Mechanism.Description = nodeValuestr;
                        }
                        else if (nodeTitle == "Trigger")
                        {
                            serviceVM.Mechanism.Triger = nodeValuestr;
                        }
                        else if (nodeTitle == "Controls")
                        {
                            nodeValuestr = xmlnode[0].ChildNodes[j].ChildNodes.Item(k).InnerText.Trim();
                            string[] s = nodeValuestr.Split(',');

                            int i = 0;
                            serviceVM.Mechanism.Controls = new int[s.Length];

                            foreach (var item in s)
                            {
                                string name = string.Empty;
                                string value = string.Empty;

                                if (item == "1")
                                {
                                    name = "Contact Selector";
                                    value = "1";
                                }
                                else if (item == "2")
                                {
                                    name = "Value Selector";
                                    value = "2";
                                }
                                else if (item == "3")
                                {
                                    name = "Text Box";
                                    value = "3";
                                }


                                serviceVM.Mechanism.Controls[i] = Convert.ToInt32(value);

                                i++;
                            }
                        }
                    }
                }
            }
        }

        return View("CreateService", serviceVM);
    }

服务虚拟机

public class Mechanism
{        
    public string Title { get; set; }
    public string Description { get; set; }
    public string Triger { get; set; }
    public int[] Controls { get; set; }
}

Home.cshtml

    var grid = new WebGrid(Model);

        @grid.GetHtml(tableStyle: "webgrid-table",           
            headerStyle: "webgrid-header",
            footerStyle: "webgrid-footer",
            alternatingRowStyle: "webgrid-alternating-row",
            selectedRowStyle: "webgrid-selected-row",
            rowStyle: "webgrid-row-style",
            mode: WebGridPagerModes.All,
            columns: new [] {              
                grid.Column("Id", "Id", canSort: true, style: "id"),
                grid.Column("Title", "Title", canSort: true, style: "title"),
                grid.Column(format: item =>                             
                         Html.ActionLink("Edit", "EditService","Home", new {Id = item.Id}, null))
                })      

生成的 HTML 片段

<tr>
  <td>
    Controls:
  </td>
  <td>    
    <select class="chosen-select" id="Mechanism_Controls"  multiple="multiple" name="Mechanism.Controls" style="width:350px">
      <option value="1">Contact  Selector</option>
      <option value="2">Value Selector</option>
      <option value="3">Text Box</option>
    </select>
  </td>                                        
</tr>

【问题讨论】:

    标签: jquery asp.net-mvc-4 jquery-chosen


    【解决方案1】:

    改变

    public List<SelectListItem> Controls { get; set; }
    

    public int[] Controls { get; set; }
    

    并使用

    @Html.ListBoxFor(m => m.Mechanism.Controls, ...
    

    该属性随后将返回一个包含所选值的数组

    注意DropDownListFor() 用于单选,ListBoxFor() 用于多选

    【讨论】:

    • 我已按照您的建议进行了更改,但仍然无法正常工作。查看我修改后的帖子。
    • 什么不起作用?你遇到了什么错误?您应该在问题中包含您的 POST 方法,并展示您如何在视图中添加 &lt;form&gt; 元素
    • 当用户在 Webgrid(Home.cshtml) 中单击“EditService”方法时,从 Home Controller 调用该方法会返回“CreateService”视图以及 View Model。
    • @DropDownListFor() 方法生成的 html 是什么?注释掉$(".chosen-select").chosen(); 进行测试
    • 生成的 html 已附加。两个 html 都相同,有或没有评论 $(".chosen-select").chosen();
    猜你喜欢
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多