Hello
I am using one of your checkbox examples ASP.Net Core Razor Pages: CheckBoxes (CheckBoxList) example as a base for my learning.
Now, I have a problem. I don't how to manipulate the HTTPPOST area. Your example has only one list, for me I have a whole model with multiple elements.
My ViewBag is returning a NULL, there is nothing once after I press the submit message.
my sample project has the below.
Controller:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using CheckBoxList_MVC_Core.Models;
namespace CheckBoxList_MVC_Core.Controllers
{
public class OrgController : Controller
{
// GET: OrgController
public ActionResult Index()
{
Registration NewRegistration = new Registration();
NewRegistration.OrgList = PopulateOrgs();
return View(NewRegistration);
}
[HttpPost]
public ActionResult Index(Registration registration)
{
ViewBag.Message = "Selected Items:\\n";
string Label1 = string.Empty;
foreach (SelectListItem li in registration.OrgList)
{
if (li.Selected == true)
{
ViewBag.Message += string.Format("{0}", li.Value);
}
}
return View(registration);
}
public static List<SelectListItem> PopulateOrgs()
{
string constr = @"Data Source=.;Initial Catalog=RAJESH;Integrated Security=true";
List<SelectListItem> items = new List<SelectListItem>();
using (SqlConnection con = new SqlConnection(constr))
{
string query = " SELECT Organization_Name, Organization_Code FROM Organizations";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["Organization_Name"].ToString(),
Value = sdr["Organization_Code"].ToString()
});
}
}
con.Close();
}
}
return items;
}
}
}
Model:
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CheckBoxList_MVC_Core.Models
{
public class Registration
{
public string CustomerCode { get; set; }
public string CustomerName { get; set; }
public List<SelectListItem> OrgList { get; set; }
}
}
and finally the view as:
@model CheckBoxList_MVC_Core.Models.Registration
<h4>Registration</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Index">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CustomerCode" class="control-label"></label>
<input asp-for="CustomerCode" class="form-control" />
<span asp-validation-for="CustomerCode" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CustomerName" class="control-label"></label>
<input asp-for="CustomerName" class="form-control" />
<span asp-validation-for="CustomerName" class="text-danger"></span>
</div>
<div>
<table>
@{ List<SelectListItem> OrgListing = Model.OrgList; }
@foreach (var Org in OrgListing)
{
<tr>
<td>
<input id="@Org.Value" type="checkbox" name="Fruit" value="@Org.Value" checked="@Org.Selected" />
</td>
<td>
<label for="@Org.Value">@Org.Text</label>
</td>
</tr>
}
</table>
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</form>
</div>
</div>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Message");
};
</script>
}
Please help