验证MVC中的Dropdownlist [英] Validating Dropdownlist in MVC

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

问题描述

我使用Entitiyframework绑定下拉列表,我想验证是否在点击按钮上选择了值



  public   class  DeviceModel 
{
[必需(ErrorMessage = 选择一个值)]
public SelectList DeviceListModel { get ; set ; }

[必需(ErrorMessage = 选择一个值)]
public SelectList DeviceType { get ; set ; }

}





  public  ActionResult Index()
{
List< Example> mod =( from data in obj.Examples select data)。ToList();
示例exp = new 示例();
exp.Name = - 选择 - ;
exp.ID = 0 ;
mod.Insert( 0 ,exp);
SelectList modeldata = new SelectList(mod, ID 名称 0 );
DeviceModel objmodel = new DeviceModel();
objmodel.DeviceListModel = modeldata;
列表< Gearhead_Type> types =( from data in obj.Gearhead_Type select data)。ToList();
Gearhead_Type GTypre = new Gearhead_Type();
GTypre.Name = - 选择 - ;
GTypre.ID = 0 ;
types.Insert( 0 ,GTypre);
SelectList asd = new SelectList(类型, ID 名称 0 );
objmodel.DeviceType = asd;

return 查看(objmodel);

}





 <   div     class   =  form-group >  
@ Html.Label(Gearhead Model and Type,新{@class =col-md-6 control-label,@ style =padding-left:200px})
@ Html.DropDownListFor(s => s.DeviceListModel,Model.DeviceListModel)
@ Html.ValidationMessageFor(s => s.DeviceListModel)
< / div >





但是单击按钮时验证不起作用。

解决方案

检查出来:如何使用模型验证MVC 5中的Dropdownlist [ ^ ]


您需要检查模型在您的操作中是否有效,以及是否返回相同的视图和模型,以便显示错误消息。



http:// www .asp.net / mvc / overview / older-versions / getting-started-with-aspnet-mvc4 / adding-validation-to-the-model [ ^ ] br />


如果你想要客户端验证,那么谷歌asp.net mvc不引人注意的客户端验证,你会发现演练如何启用它。


你在你的页面上添加



< script src =@ Url.Content(〜 /Scripts/jquery.validate.min.js\")\"></script>

< script src =@ Url.Content(〜/ Scripts / jquery.validate.unobtrusive。 min.js ) >< /脚本>

I am binding a dropdown using Entitiyframework and i want to validate if the value is selected or not on a button click

public class DeviceModel
   {
       [Required(ErrorMessage = "Select a value")]
       public SelectList DeviceListModel { get; set; }

       [Required(ErrorMessage = "Select a value")]
       public SelectList DeviceType { get; set; }

   }



public ActionResult Index()
      {
          List<Example> mod = (from data in obj.Examples select data).ToList();
          Example exp = new Example();
          exp.Name = "--Select--";
          exp.ID = 0;
          mod.Insert(0, exp);
          SelectList modeldata = new SelectList(mod, "ID", "Name", 0);
          DeviceModel objmodel = new DeviceModel();
          objmodel.DeviceListModel = modeldata;
          List<Gearhead_Type> types = (from data in obj.Gearhead_Type select data).ToList();
          Gearhead_Type GTypre = new Gearhead_Type();
          GTypre.Name = "--Select--";
          GTypre.ID = 0;
          types.Insert(0, GTypre);
          SelectList asd = new SelectList(types, "ID", "Name", 0);
          objmodel.DeviceType = asd;

          return View(objmodel);

      }



<div class="form-group">
               @Html.Label("Gearhead Model and Type  ", new { @class = "col-md-6 control-label ", @style = "padding-left:200px" })
               @Html.DropDownListFor(s=>s.DeviceListModel,  Model.DeviceListModel)
               @Html.ValidationMessageFor(s => s.DeviceListModel)
           </div>



But the validation is not working in the button click.

解决方案

Check this out: How to validate Dropdownlist in MVC 5 using model[^]


You need to check if the model is valid in your action and if it isn't return the same view and model so the error messages can be shown.

http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-validation-to-the-model[^]

If you want client-side validation also then google "asp.net mvc unobtrusive client-side validation" and you'll find walkthroughs on how to enable it.


you Add On Your Page

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>


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

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