本文介绍了如何验证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>
}

推荐答案

<script type="text/javascript">




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

08-20 22:07