|
<form asp-controller="Home" asp-action="Index" method="post">
<ejs-checkbox id="cbox2" name="Sport" value="Cricket" label="Cricket" checked="true"></ejs-checkbox>
<ejs-checkbox id="cbox3" name="Sport" value="Hockey" label="Hockey" checked="true"></ejs-checkbox>
<ejs-checkbox id="cbox4" name="Sport" value="Tennis" label="Tennis"></ejs-checkbox>
<ejs-checkbox id="cbox1" name="Sport" value="Basketball" label="Basketball"></ejs-checkbox>
<ejs-button id="primarybtn" content="Submit" isPrimary="true"></ejs-button>
</form>
|
|
[HttpPost]
public IActionResult Index(SportModel model)
{
//Here you can get the selected checkbox values.
var selectedChkBxValues = model.Sport;
//Your code here.
}
//...
public class SportModel
{
public List<string> Sport { get; set; }
}
|
|
Return Values |
Return Type |
Description |
|
data |
Object | string[] |
It denotes the Selected Item dataSource JSON object.
Here We can retire id, text etc. |
|
Item |
HTMLElement | Element |
It denotes the Selected Item list element. |
|
text |
string |
It denotes the Selected Item text. |
|
Return Values |
Return Type |
Description |
|
Data |
Object | Object[] | string[] |
It denotes the Selected Item dataSource JSON object or object collection.
Here We can retire id, text etc. |
|
Item |
HTMLElement | Element[] | HTMLCollection |
It denotes the Selected Item list element or element collection. |
|
text |
string | string[] |
It denotes the Selected Item text data or collection. |
|
@{
ViewBag.Title = "Welcome to My Index Page";
}
<h1>Wecome to Forms Page</h1><br />
<form asp-controller="MyCal" method="post">
<ejs-listview id="list" dataSource="@ViewBag.dataSource" showCheckBox="true" showHeader="true" headerTitle="Types Of Art"></ejs-listview>
<br />
<label for="art">Type of art you are in to: </label>
<input id="art" type="text" name="art" />
<br />
<input type="submit" id="button" value="Submit" formaction="GetValues" />
</form>
<h2 style="color:crimson; background-color:darksalmon; padding:10px;">
@ViewBag.Result
</h2>
<script>
document.getElementById('button').addEventListener('click', function () {
// Obtaining Listview Instance
var listviewInstance = document.getElementById('list').ej2_instances[0];
var texts = "";
for (var i = 0; i < listviewInstance.getSelectedItems().text.length; i++) {
// Assigning each value selected in the listview
texts = texts + listviewInstance.getSelectedItems().text[i] + ", ";
}
// Removes extra comma
texts = texts.replace(/(^,)|(, $)/g, "");
// Assigning listview value to text box
var textbox = document.getElementById('art');
textbox.value = texts;
})
</script>
|
|
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using WebApplication2.Controllers;
namespace Listview.Controllers
{
public class MyCalController : Controller
{
// GET: /<controller>/
public IActionResult Lists()
{
//define the array of JSON
List<object> data = new List<object>();
data.Add(new { text = "Artwork", id = "01" });
data.Add(new { text = "Abstract", id = "02" });
data.Add(new { text = "Modern Painting", id = "03" });
data.Add(new { text = "Oil Painting", id = "04" });
ViewBag.dataSource = data;
return View();
}
[HttpPost]
public IActionResult GetValues()
{
string value = HttpContext.Request.Form["art"];
ViewBag.Result = value;
return View("Values");
}
}
}
|
Yuck. Sorry, but this is a horrible way to save data back to the controller. You're circumventing all the great features of MVC and Razor that could perform the above in only a single line of code on both the client and server and instead are doing everything manually in Javascript and then capturing the request body, like 1990s PHP. You guys need to start binding the listview component in the view to a form element so that it can get easily posted back.
Hi Bwmmc,
namespace CoreSample.Models { public class ArtModel { public List<string> SelectedArt { get; set; } } } |
... namespace CoreSample.Controllers { public class HomeController : Controller { public IActionResult Index() { LoadData(); // Load data when rendering the Index page return View(); } public IActionResult Privacy() { return View(); } [HttpPost] public IActionResult GetValues(ArtModel model) { if (model.SelectedArt != null && model.SelectedArt.Any()) { ViewBag.Result = string.Join(", ", model.SelectedArt); } else { ViewBag.Result = "No items selected"; } LoadData(); // Load data again when rendering the view after form submission // Return the same Index view return View("Index", model); } private void LoadData() { List<object> data = new List<object> { new { text = "Artwork", id = "01" }, new { text = "Abstract", id = "02" }, new { text = "Modern Painting", id = "03" }, new { text = "Oil Painting", id = "04" } }; ViewBag.dataSource = data; } } } ... |
@{ ViewBag.Title = "Welcome to My Index Page"; } @using Syncfusion.EJ2 @model CoreSample.Models.ArtModel <h1>Welcome to Forms Page</h1> <br /> <form asp-controller="Home" asp-action="GetValues" method="post"> <ejs-listview id="list" dataSource="@ViewBag.dataSource" showCheckBox="true" showHeader="true" headerTitle="Types Of Art"></ejs-listview> @Html.HiddenFor(model => model.SelectedArt) <br /> <input type="submit" value="Submit" /> </form> <h2 style="color:crimson; background-color:darksalmon; padding:10px;"> @ViewBag.Result </h2> <script> document.querySelector('form').addEventListener('submit', function () { var listviewInstance = document.getElementById('list').ej2_instances[0]; var selectedItems = listviewInstance.getSelectedItems().text; document.querySelector('[name="SelectedArt"]').value = selectedItems.join(','); }); </script> |
Before form submission | After form submission |
Regards,
Prasanth Madhaiyan.