Thursday 14 January 2021

MVC - How to make Drop Down List required field

 Watch this example on YouTube


 

1. Model


using System.ComponentModel.DataAnnotations;
namespace WebApplication1.Models
{
    using System;
    
    public partial class SelectOrders_Result
    {
        public int OrderID { get; set; }

        [Required]
        public Nullable<int> ProductID { get; set; }
        public Nullable<int> Qty { get; set; }
        public bool test { get; set; }
        public Nullable<bool> Column1 { get; set; }
        public Nullable<int> Column2 { get; set; }
        public string Column3 { get; set; }
    }
}
 

 

2. View

@model WebApplication1.Models.OrderCustomClass

@using (Html.BeginForm("Index", "Home"))
{
  @Html.DropDownListFor(x=>x.or.ProductID, new SelectList(Model.prod, "ProductID", "Name", Model.prod), "")
 @Html.ValidationMessageFor(x=>x.or.ProductID)
    <input type="submit"  value="Submit" />
}

No comments:

Post a Comment