Hi,
I am new to MVC. Direct to the question, as I listing the cities via checkboxes. When the HTTPPost method triggers the Passing List checked value become null.
Refered Below Link for this demo
http://csharp-video-tutorials.blogspot.in/2013/06/part-38-checkboxlist-in-aspnet-mvc.html
I don't know where I done wrong.
Edit Template (city.cshtml) which is available in View/Home/EditTemplate Folder
@model MVC25122017.Models.City
@Html.HiddenFor(x=>x.ID)
@Html.HiddenFor(x => x.Cities)
@Html.CheckBoxFor(x=>x.Isselected.Value)
@Html.DisplayFor(x=>x.Cities)
Sql Table script for creating and added this table via ADO.Net Entity Data Model
CREATE TABLE [dbo].[Cities](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Cities] [varchar](50) NULL,
[Isselected] [bit] NULL,
CONSTRAINT [PK_Cities] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
values for above tables are below
ID Cities Isselected
1 chennai true
2 Delhi false
3 bangalore false
Calling EditTemplate in the main view (View/Home/Index0.cshtml)
@model IEnumerable
@using (Html.BeginForm())
{
@Html.EditorForModel()
}
Controller - Controller/Homecontroller.cs
[HttpGet]
public ActionResult Index0()
{
CitiesDBContext cities = new CitiesDBContext();
return View(cities.Cities);
}
[HttpPost]
public string Index0(IEnumerable cities)
{
StringBuilder sb = new StringBuilder();
sb.Append("Your Selected cities are -");
if (cities.Count(x => x.Isselected.Value)==0)
{
return "You did not select any cities";
}
else
{
foreach (City city in cities)
{
if (city.Isselected == true)
{
sb.Append(city.Cities + ",");
}
}
sb.Remove(sb.ToString().LastIndexOf(","), 1);
return (sb.ToString());
}
}
Please let me know where I am doing wrong.