当用户更改ASP中的下拉值时打开弹出窗口

时间:2014-04-01 14:11:53

标签: c# html asp.net asp.net-mvc asp.net-mvc-4

我创建了一个MVC5应用程序,并使用以下代码创建一个下拉列表。它有2个值,如女性和男性,默认值为男性。
每当用户更改下拉值时,我想打开一个带文本字段的弹出窗口,我应该怎么做?

索引代码:

<td>
            @Html.DropDownListFor(modelItem => item.Genere, item.Genere)
        </td>

型号代码:

  public IEnumerable<SelectListItem> Genere
        {
            get
            {
                return new[]
                {
                    new SelectListItem {Value = "M", Text = "Male"},
                    new SelectListItem {Value = "F", Text = "Female"}
                };
            }
        }

更新视图代码

@model IEnumerable<Admin.Models.Admin>
<script src='https://code.jquery.com/jquery-1.11.0.min.js'></script>
<script src='https://code.jquery.com/ui/1.9.2/jquery-ui.min.js'></script>


<script>
    $(document).ready(function () {
        var dialog = $('#dialog').dialog({
        });


        $('#SystemType').change(function () {
            //if($(this).val()=='F')
            dialog.dialog('open');
        });
    });
</script>
<h3>My APP</h3>


p>
    @using (Html.BeginForm())
    {

    }


    @*<br style="margin-bottom:240px;" />*@
    @Html.ActionLink("Create", "Create",
        null, htmlAttributes: new { @class = "mybtn" })
    <p>

    </p>


    <style type="text/css">
        a.mybtn {
            background: #fa0088;


        }
    </style>

  <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Email)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Geneder)
     </th>
            <th></th>
        </tr>


   @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Email)
                </td>
                <td>
                    @Html.DropDownListFor(modelItem => item.Geneder, item.Geneder, new { id = "M" })

                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                    @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                </td>

            </tr>

    <script type="text/javascript">

        $(document).ready(function () {
            $('#M').change(function () {
                if ($(this).val() === "F") {
                    dialog.dialog('open');
                }
            });
        });

    </script>

2 个答案:

答案 0 :(得分:2)

你可以借助jQuery来做到这一点。希望您已经在MVC 5应用程序中使用jQuery

假设在_layout.cshtml中

<head>
<script src="@Url.Content("/scripts/jquery.js")" type="text/javascript" />

在视图

@Html.DropDownListFor(modelItem => item.Genere, item.Genere, new {id="ddlGender"})

的jQuery

$(document).ready(function(){
  $('#ddlGender').change(function(){
   if($(this).val()==="F")
   {
    //code for opening popup
   }
 });
});

答案 1 :(得分:2)

使用jQuery&amp; JQueryUI添加到您的视图中,在视图中添加此代码

靠近head标记

  <script src='https://code.jquery.com/jquery-1.11.0.min.js'></script>
  <script src='https://code.jquery.com/ui/1.9.2/jquery-ui.min.js'></script>

 <script>
 $(document).ready(function(){
       var  dialog=$('#dialog').dialog({
                                    hide:true
       });


     $('#Genere').change(function(){
       //if($(this).val()=='F')
        dialog.dialog('open');
      });
  });
  </script>
 </head>
 <body>
   <!-- ----     ------ -->

 <td>
        @Html.DropDownListFor(modelItem => item.Genere, item.Genere)
    </td>
   <!-- ----     ------ -->

并在视图的末尾添加此html

<div id='dialog'><input type='text' id='textb' class='textb'/></div>