We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to show selected value in DropdownList through Viewbag and how to validate it

Here is my code of View : 

@Html.LabelFor(model => model.OperationId, "Operation Name ", new { @class = "control-label" })<b class="mandatory"> * </b><b>:</b>
@Html.EJS().DropDownList("OperationId").Placeholder("Select a Operation").FilterBarPlaceholder("Search Operation").AllowFiltering(true).DataSource((List<OperationModel>)ViewBag.OperationId).Filtering("onfiltering").Fields(new DropDownListFieldSettings { Text = "OperationName", Value = "OperationId" }).Render()        
@Html.ValidationMessageFor(model => model.OperationId, "", new { @class = "text-danger" })

1 Reply

CI Christopher Issac Sunder K Syncfusion Team November 15, 2018 10:13 AM UTC

Hi Rajesh, 
 
Thank you for contacting Syncfusion Support. 
 
We have prepared a sample similar to your requirement by passing pre-selected value from ViewBag and added validation for DropDownList control.  
 
@model Uploader.Controllers.OperationModel 
 
@using (Html.BeginForm("Register", "Home", FormMethod.Post, null)) 
{ 
    @Html.LabelFor(model => model.OperationId, "Operation Name ", new { @class = "control-label" })<b class="mandatory"> * </b><b>:</b> 
    @Html.EJS().DropDownList("OperationId").Width("300").ShowClearButton(true).Placeholder("Select a Operation").FilterBarPlaceholder("Search Operation").AllowFiltering(true).DataSource((List<OperationModel>)ViewBag.OperationId).Fields(new DropDownListFieldSettings { Text = "OperationName", Value = "OperationId" }).Value(ViewBag.Value).Render() 
    @Html.ValidationMessageFor(model => model.OperationId, "", new { @class = "text-danger" }) 
    <br /> 
    <input type="submit" value="Save" class="btn btn-default" /> 
} 
 
 
Controller: 
public class HomeController : Controller 
    { 
        OperationModel model = new OperationModel(); 
        public ActionResult Index() 
        {             
            ViewBag.OperationId= OperationModel.GetAllRecords(); 
            ViewBag.Value = "1006"; 
            return View(); 
        } 
 
        public ActionResult Register(OperationModel model) 
        { 
            ViewBag.OperationId = OperationModel.GetAllRecords(); 
            if (ModelState.IsValid) 
            { 
               //perform required action after validation. 
            }           
            return View("Index"); 
        } 
    } 
     
    public class OperationModel 
    { 
        [Required(ErrorMessage ="Please Select an Item")] 
        public string OperationId { get; set; } 
       
        public string OperationName { get; set; } 
 
        public OperationModel() 
        { 
        } 
        public OperationModel(string OperationId, string OperationName) 
        { 
            this.OperationId = OperationId; 
            this.OperationName = OperationName; 
        } 
        public static List<OperationModel> GetAllRecords() 
        { 
            List<OperationModel> order = new List<OperationModel>(); 
                order.Add(new OperationModel("1001", "ALFKI")); 
                order.Add(new OperationModel("1002", "ANATR")); 
                order.Add(new OperationModel("1003", "ANTON")); 
                order.Add(new OperationModel("1004", "BLONP")); 
                order.Add(new OperationModel("1005", "BOLID")); 
                order.Add(new OperationModel("1006", "MARTIN")); 
                order.Add(new OperationModel("1007", "CACTU")); 
                order.Add(new OperationModel("1008", "VINCET")); 
                order.Add(new OperationModel("1009", "JOHN")); 
                order.Add(new OperationModel("1010", "MATHEW")); 
          
            return order; 
        }       
    } 
 

Please let us know if you need any further assistance. 

Thanks,
Christo


Loader.
Live Chat Icon For mobile
Up arrow icon