DropDownListFor不触发OnChange事件

时间:2014-09-10 10:22:52

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

当用户点击home.cshtml的webgrid中的编辑选项时," EditService"在home控制器中调用方法,它返回" CreateService"查看其视图模型。一切正常。但问题是DropDownListFor没有触发onchange事件,尽管值已更改为指定。调用此视图时,我需要自动触发此事件。

查看代码段

@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
    Text = "One Shot",
    Value = "1"
});
listItems.Add(new SelectListItem
{
    Text = "Subscription",
    Value = "2"
});
}

@Html.DropDownListFor(m => m.ServiceType, listItems, new { @class = "chosen-select",  @style =  "width:120px" , @id="cmbServiceType"})


<script>
$(document).on('change', '#cmbServiceType', function () {

    alert($("#cmbServiceType option:selected").val());

    if ($("#cmbServiceType option:selected").val() == "2") {

        //sms mode is selected

        $('#lblequipId').show();
        $('#txtequipId').show();

    }
    else {
        //dial mode is selected

        $('#lblequipId').hide();
        $('#txtequipId').hide();
    }
});

</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 == "LONGDESC")
                {
                    serviceVM.Content = nodeValuestr;
                }
                else if (nodeTitle == "DETAIL")
                {
                    serviceVM.Detail = nodeValuestr;
                }
                else if (nodeTitle == "EQUIPID")
                {
                    serviceVM.EquipId = nodeValuestr;

                    if (nodeValuestr == "0")
                    {
                        serviceVM.ServiceType = 1;
                    }
                    else
                    {
                        serviceVM.ServiceType = 2;
                    }
                }
                else if (nodeTitle == "MODE")
                {
                    if (nodeValuestr == "Dial")
                        serviceVM.Mode.ModeType = 1;
                    else
                        serviceVM.Mode.ModeType = 2;
                }
                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++;

                                ////Create the select list item you want to add
                                //SelectListItem selListItem = new SelectListItem() {Text = name, Value = value };

                                ////Create a list of select list items - this will be returned as your select list
                                //serviceVM.Mechanism.Controls = new List<SelectListItem>();

                                ////Add select list item to list of selectlistitems
                                //serviceVM.Mechanism.Controls.Add(selListItem);
                            }
                        }
                    }
                }
            }
        }

        return View("CreateService", serviceVM);
    }

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

1 个答案:

答案 0 :(得分:1)

您可以使用.trigger()方法触发事件

<script>
  $(document).on('change', '#cmbServiceType', function () {
    if ($("#cmbServiceType option:selected").val() == "2") {
      ....
    } else {
      ....
    }
  });

  $('#cmbServiceType').trigger('change'); // add this

</script>