Asp .Net在客户端检查两个下拉列表的值

时间:2017-06-29 17:09:00

标签: c# drop-down-menu

我有两个下拉列表,我想检查以下内容 如果DDL1_Value1 == 1&& DDL2_Value2 == 2 将通知用户该组合无效。代码如下所示:

<asp:DropDownList ID="DDL_Value1" runat="server" CssClass="stdDropdownSmall" OnSelectedIndexChanged="ddlGxP_SelectedIndexChanged" AutoPostBack="true" />
    <asp:RequiredFieldValidator ID="RFVddlGxP" runat="server" ControlToValidate="DDL_Value1" InitialValue="0" CssClass="RequiredFieldError" ErrorMessage=" ! Please insert"  />

}   其他   {               }     

第二个下拉列表的代码如下:             

      OnServerValidate="cvFinalizedMethod_ServerValidate" CssClass="RequiredFieldError" Text="! Please select another Standard"   />

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作。

查看:

@model Testy20161006.Controllers.YourViewModel
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>IndexValid9</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".check").change(function () {
                if ($("#ddlOne").val() == "1" && $("#ddlTwo").val() == "2") {
                    alert("disallowed selection!")
                }
            })
        })
    </script>
</head>
<body>
    @using (Html.BeginForm())
    {
        <table>
            <tr>
                <td>
                    @Html.DropDownListFor(m => m.SelectedA,
                    new SelectList(Model.TableAList, "Value", "Text"), new { id = "ddlOne", @class = "check" })
                    @Html.ValidationMessageFor(model => model.SelectedA)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.DropDownListFor(m => m.SelectedB,
                    new SelectList(Model.TableAList, "Value", "Text"), new { id = "ddlTwo", @class = "check" })
                    @Html.ValidationMessageFor(model => model.SelectedB)
                </td>
            </tr>
        </table>
        <input type="submit" value="submit" />
    }
</body>
</html>

控制器/视图模型:

//You can put this in its own folder
public class Utilites
{
    public static List<SelectListItem> GetTableAList()
    {
        List<SelectListItem> list = new List<SelectListItem>();

        SelectListItem item1 = new SelectListItem { Text = "one", Value = "1" };
        SelectListItem item2 = new SelectListItem { Text = "two", Value = "2" };
        SelectListItem item3 = new SelectListItem { Text = "three", Value = "3" };
        list.Add(item1);
        list.Add(item2);
        list.Add(item3);

        //add <select> to first item
        list.Insert(0, new SelectListItem { Text = "", Value = "" });

        return list;
    }

    public static List<SelectListItem> GetTableBList()
    {
        List<SelectListItem> list = new List<SelectListItem>();

        SelectListItem item1 = new SelectListItem { Text = "one", Value = "1" };
        SelectListItem item2 = new SelectListItem { Text = "two", Value = "2" };
        SelectListItem item3 = new SelectListItem { Text = "three", Value = "3" };
        list.Add(item1);
        list.Add(item2);
        list.Add(item3);

        //add <select> to first item
        list.Insert(0, new SelectListItem { Text = "", Value = "" });

        return list;
    }
}

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult IndexValid9(YourViewModel yourViewModel)
    {
       //you can put your breakpoint here to see user selected values
       return View(yourViewModel);
    }

    public ActionResult IndexValid9()
    {
        YourViewModel yourViewModel = new YourViewModel();
        return View(yourViewModel);
    }