Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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).

C#
//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>
}
Posted
Comments
John C Rayan 9-Aug-15 3:19am    
You need jquery to achieve this
Aakash Bashyal 9-Aug-15 4:17am    
Where is Jquery ?
John C Rayan 9-Aug-15 8:16am    
I do not follow you. Have you worked on jquery before? if not , you need to read and understand first. Do you want to know the site for jquery?

1 solution

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


JavaScript
<script type="text/javascript">
    $(document).ready(function () {
        $("#btnNew").click(function (e) {
            if ($("#Month_id").val() == null || $("#Month_id").val() == "") {
                $("[data-valmsg-for='Month_id']").text("Required");
                e.preventDefault();
            } else {
                $("[data-valmsg-for='Month_id']").text("");
                e.preventDefault.remove();
            }
        });
    });
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900