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.