How to add list box content to a submit

Hi,

how can I add listbox items to a submit so my controller can get it?

{code}

        Html.EJ().ListBox("allWorkflowSteps").Datasource((IEnumerable<WorkflowStep>)ViewBag.AllWorkflowSteps)
                        .ListBoxFields(a => a.Text("Name"))
                        .AllowDrag(true).AllowDrop(true)


        public ActionResult Create([Bind(Include = "Id,Name")] Product product, string allWorkflowSteps)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(product);
        }

{code}


Cheers,

Jonas


1 Reply

BS Buvana Sathasivam Syncfusion Team January 9, 2018 09:29 AM UTC

Hi Jonas,   
  
Thanks for using Syncfusion Products.   
  
We have created ListBoxFor sample with datasource fields.  After selecting listbox value, if you click create button, create post method is triggered and selected value is passed to the controller tosave data into database.  Please find the below code.      
 
We have used viewbag data source into listbox datasource which contains Id and Name fields.      
  
Create.cshtml    
    
@Html.EJ().ListBoxFor(model => model.Name).Datasource((IEnumerable<object>)ViewBag.datasource).ListBoxFields(df => df.ID("Id").Text("Name"))  // ListBox For   
      
  
We have created Listdatas controller using Listdata model class in which model contains Id and Name fields.  Now, we have saved listbox selected value(Name) into Listdatas table.  Please find the below code.        
  
TasksController.cs    
   
     private NORTHWNDEntities db = new NORTHWNDEntities(); // Database   
   
      List<Names> Name = new List<Names>();   // List   
           
        public ActionResult Create()    
        {   
            Name.Add(new Names { Id = "1", Name = "Sansa" });   
            …………..   
           ViewBag.datasource = Name;  // Listbox data   
            return View();   
        }   
   
        [HttpPost]   
        [ValidateAntiForgeryToken]   
        public ActionResult Create([Bind(Include = "Id,Name")] Listdata listdata)   
        {   // Triggered after clicked create button   
            if (ModelState.IsValid)   
            {   
                db.Listdatas.Add(listdata); // Add data into listdatas table   
                db.SaveChanges();           // Save the data   
                return RedirectToAction("Index");     
            }   
   
            return View(listdata);   
        }   
   
   
   
  
Regards,   
Buvana S.   
   
 


Loader.
Up arrow icon