如何验证一个下拉列表项已选定 [英] How to validate that a dropdown list item has been selected

查看:51
本文介绍了如何验证一个下拉列表项已选定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,这一定是容易的,但是......

我有一个下拉列表,在我看来:

 <%= Html.DropDownList(ddlDistricts
Model.clients.DistrictList, - 选择地区 -
新{@class =ddltext,风格=宽度:200像素的onchange =this.form.submit(); })%GT;

Model.clients.DistrictList是类型的SelectList的。

我想要做的就是确保用户选择的东西(即---选择district--,其中有的值,未选择)。

因此​​,在控制我有:

 的[AcceptVerbs(HttpVerbs.Post)
    公众的ActionResult指数(字符串日期,字符串月份的FormCollection形式)
    {         如果(表格[chooseSchool] ==提交)//点击提交按钮
        {
                如果(String.IsNullOrEmpty(表格[ddlDistrict]))
                {
                    ModelState.AddModelError(ddlDistrict,请选择一个区);
                }
                其他
                {
                    //存储一些数据的地方.......                }        }         //创建modelData对象.....         返回查看(modelData);     }

但是,什么情况是存在,当它试图重新显示视图,一个空对象异常显然是因为

的ModelState [ddlDistricts。值为空,所以不能适用的ModelState [ddlDistricts。Value.AttemptedValue的下拉列表中的值。

据我已阅读,在试图提供值字段时ModelState.IsValid是假的,它试图与按此顺序错误的控制提供一个值:

(1)的ModelState [字段名]。Value.AttemptedValue

(2)控制明文规定值

(3)的ViewData

因此​​,正在申请的ModelState,但Value属性为空,所以试图访问AttemptedValue生成异常。

什么是这个问题的答案?你怎么能检查,以确保合法的项目已经从一个DropDownList选择?

我敢肯定,这很容易,但我似乎无法使用的ModelState错误处理方案来做到这一点。


解决方案

  ModelState.AddModelError(ddlDistrict,请选择一个区);
ModelState.SetModelValue(ddlDistrict,ValueProvider [ddlDistrict]);

Well, this must be easy, but ...

I have a dropdown list in my view:

<%= Html.DropDownList("ddlDistricts", 
Model.clients.DistrictList,"-- select district --", 
new { @class = "ddltext", style="width: 200px", onchange = "this.form.submit();" }) %>

Model.clients.DistrictList is of type SelectList.

What I want to do is make sure the user selects something (i.e., "--- select district-- ", which has a value of "", is not selected).

So in the controller I have:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(string date, string month, FormCollection form)
    {

         if (form["chooseSchool"] == "Submit")  // submit button is clicked
        {
                if (String.IsNullOrEmpty(form["ddlDistrict"]))
                {
                    ModelState.AddModelError("ddlDistrict", "Please select a district");
                }
                else
                {
                    // store some data somewhere .......

                }

        }

         // create the modelData object .....

         return View(modelData);

     }

But what happens is there is a null object exception when it attempts to redisplay the view, apparently because

ModelState["ddlDistricts"].Value is null, so it can't apply ModelState["ddlDistricts"].Value.AttemptedValue as the value of the dropdown list.

According to what I have read, in attempting to supply a value to a field when ModelState.IsValid is false, it attempts to provide a value for the control with the error in this order:

(1) ModelState["fieldname"].Value.AttemptedValue

(2) Explicitly provided value in control

(3) ViewData

So it is applying ModelState, but the Value property is null, so trying to access AttemptedValue generates an exception.

What is the answer to this? How can you check to make sure a legitimate item has been selected from a DropDownList?

I'm sure it's easy, but I can't seem to do it using the ModelState error-handling scheme.

解决方案

ModelState.AddModelError("ddlDistrict", "Please select a district");
ModelState.SetModelValue("ddlDistrict", ValueProvider["ddlDistrict"]);

这篇关于如何验证一个下拉列表项已选定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆