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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:





140938 Nov 15,2018 07:52 AM UTC Nov 15,2018 10:13 AM UTC ASP.NET MVC - EJ 2 1
Tags: DropDownList
Asked On November 15, 2018 07:52 AM UTC

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" })

Christopher Issac Sunder K [Syncfusion]
Replied On 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" /> 
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. 



This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon