如何验证Asp .net MVC中的DropDown列表 [英] How To Validate DropDown List in Asp .net MVC

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

问题描述

在添加员工薪水之前,我必须从下拉列表中选择月份。月份列表存储在数据库中。如果未选择月份且用户按下创建按钮,则程序应显示未选择月份的消息。我怎样才能做到这一点?如果选择月份并且用户重定向到创建表单,则所选月份的ID应存储在Salary表的(month_id)列中。 (即month_id =所选月份的id)。



Before Adding the salary of Employee, I had to Select Month from the Drop Down List. The list of the Month are stored in databases. if the month is not selected and user press Create Button the program should display the message that no Month is selected. How Can i achieve this ? If the month is selected and user redirects to create form then the id of the selected month should be stored in (month_id) column of the Salary table. ( i.e month_id = id of the Selected month).

//Model
public class Salary
{
   public int id { get; set; }
   public Nullable<int> month_id { get; set; }

   [Required]
   public virtual Month Month { get; set; }

   public IEnumerable<Month> GetMonths()
   {
        IEnumerable<Month> mat = null;
        mat = this.db.Months.ToList();
        return mat;
   }

}
Public Class Month
{
    public int id { get; set; }
    public string month { get; set; }
    public virtual ICollection<Salary> Salary { get; set; }
}



控制器:


controller :

public ActionResult Index()
    {
        Salary salary = new Salary();
        ViewData["monthType"] = salary .GetMonths().ToList().Select(
                 s => new SelectListItem
                 {
                     Text = s.month,
                     Value = s.id.ToString()
                 });

        return View(salary);
    }



浏览次数:


Views:

@using(Html.BeginForm("Create","Talab",FormMethod.Post))
{ 
    <div class="row">
        <div class="form-group">
            <div class="col-md-2">
                <a href="@Url.Action("Create","TotalSalary")" class="btn btn-success input-sm">Add New </a>
            </div>
        </div>
        <div class="form-group">   
            <div class="col-md-2">
                @Html.DropDownListFor(model => model.Month.id,     (IEnumerable<SelectListItem>)ViewData["monthType"], "--Select a Month--")
                @Html.ValidationMessageFor(model => model.Month.id)
            </div>
        </div>
    </div>
}

推荐答案





你能达到在jquery的帮助下你需要包含jquery ref。任何最小的jquery。



ie< script src =〜/ Scripts / jquery-1.10.2.min.js>< / script>



之后将id添加到href链接



并在视图页面的代码下面写下来,即





Hi,

you can achieve with help of jquery for that you need to include jquery ref. any min jquery.

i.e <script src="~/Scripts/jquery-1.10.2.min.js"></script>

After that add "id" to href link

and write it down below code in your view page i.e


<script type="text/javascript">


document )。 ready( function (){
(document).ready(function () {


#btnNew)。click( function (e){
if
("#btnNew").click(function (e) { if (


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

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