下拉列表验证出错 [英] Error in Dropdownlist validation

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

问题描述

嗨朋友们,

我需要在mvc 4中验证模型中的下拉列表。我在这里发布了我的代码。



我的模特

 查看 

@ Html.LabelFor(M => Model.Patient.DoctorID)
@ Html.DropDownList(doctorType)


控制器

public List< SelectListItem> SelectDoctor()
{
Gema_Doctor [] doctorList;
doctorList = gema_Doctor.GetDoctor()。ToArray();
List< gema_doctor> listDoctor = new List< gema_doctor>(doctorList);
List< SelectListItem> dropItems = new List< SelectListItem>();
dropItems.Add(new SelectListItem {Text = - Select - ,Value =,Selected = true});
foreach(gema_Doctor doctorValues in listDoctor)
{
dropItems.Add(new SelectListItem {Text = doctorValues.Dr_Name,Value = doctorValues.Dr_Id.ToString()});
}
return dropItems;
}。

模型
[必填(ErrorMessage =请选择医生)]
[显示(名称=医生ID)]
public int DoctorID {get;组; }

解决方案

你可以用这个javascript



onclick按钮的功能写一个javascript来检查下拉列表中的值是什么,根据该显示消息



如果没有那么只是告诉我你是什么恐怖消息获得

Hi Friends,
I need to validate the dropdownlist in model at mvc 4 . I have posted my code here .

My Model

        View

      @Html.LabelFor(M => Model.Patient.DoctorID)
      @Html.DropDownList("doctorType")


             Controller

      public List<SelectListItem> SelectDoctor()
{
    Gema_Doctor[] doctorList;
    doctorList = gema_Doctor.GetDoctor().ToArray();
    List<gema_doctor> listDoctor = new List<gema_doctor>(doctorList);
    List<SelectListItem> dropItems = new List<SelectListItem>();
    dropItems.Add(new SelectListItem { Text = "--Select--", Value = "", Selected = true });
    foreach (Gema_Doctor doctorValues in listDoctor)
    {
        dropItems.Add(new SelectListItem { Text = doctorValues.Dr_Name, Value = doctorValues.Dr_Id.ToString() });
    }
    return dropItems;
}  .

              Model
     [Required(ErrorMessage="Please Select Doctor")]
     [Display(Name = "DoctorID")]
     public int DoctorID { get; set; }

解决方案

you can use javascript for this

onclick function of button write a javascript to check what is the value in dropdown list according to that show message

if not then just show me what isthe drror message you are getting


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

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