Part 38 CheckBoxList in asp net mvc

  Рет қаралды 179,315

kudvenkat

kudvenkat

Күн бұрын

In this video we will discuss implementing a checkbox list in asp.net mvc. We will be using table "tblCity" for this demo.
Using the link below for, Sql script to create table tblCity
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our KZbin channel. Hope you can help.
/ @aarvikitchen5572
Add ADO.NET data model, to retrieve data from database table tblCity
Right click on the "Controllers" folder, and add a "HomeController". Include the following 2 namespaces in "HomeController"
using MVCDemo.Models;
using System.Text;
Copy and paste the following code.
[HttpGet]
public ActionResult Index()
{
SampleDBContext db = new SampleDBContext();
return View(db.Cities);
}
[HttpPost]
public string Index(IEnumerable[City] cities)
{
if (cities.Count(x =] x.IsSelected) == 0)
{
return "You have not selected any City";
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append("You selected - ");
foreach (City city in cities)
{
if (city.IsSelected)
{
sb.Append(city.Name + ", ");
}
}
sb.Remove(sb.ToString().LastIndexOf(","), 1);
return sb.ToString();
}
}
Right click on the "Views" folder, and a "Home" folder. Right click on the "Home" folder and "EditorTemplates" folder.
Right click on "EditorTemplates" folder - Add - View. In the "Add View" dialog box, set
View Name = City
View Engine = Razor
and click "Add".
Copy and paste the following code in "City.cshtml"
@model MVCDemo.Models.City
@{
ViewBag.Title = "City";
}
@Html.HiddenFor(x =] x.ID)
@Html.HiddenFor(x =] x.Name)
@Html.CheckBoxFor(x =] x.IsSelected)
@Html.DisplayFor(x =] x.Name)
Please Note: Put the templates in "Shared" folder, if you want the "Templates", to be available for all the views.
Right click on the "Index" action method in "HomeController", and select "Add View" from the contex menu. Set
View Name = Index
View Engine = Razor and click "Add"
Copy and paste the following code in "Index.cshtml"
@model IEnumerable[MVCDemo.Models.City]
@{
ViewBag.Title = "Index";
}
[div style="font-family:Arial"]
[h2]Index[/h2]
@using (Html.BeginForm())
{
@Html.EditorForModel()
[br /]
[input type="submit" value="Submit" /]
}
[/div]
Text version of the video
csharp-video-tu...
Slides
csharp-video-tu...
All ASP .NET MVC Text Articles
csharp-video-tu...
All ASP .NET MVC Slides
csharp-video-tu...
All Dot Net and SQL Server Tutorials in English
www.youtube.co...
All Dot Net and SQL Server Tutorials in Arabic
/ kudvenkatarabic

Пікірлер: 94
@sushmaka4905
@sushmaka4905 10 жыл бұрын
Hi Venkat Thank you for your videos!They are very clear to understand and your teaching is really good!You explain every concept in a clear and easy way!
@Csharp-video-tutorialsBlogspot
@Csharp-video-tutorialsBlogspot 11 жыл бұрын
Thank you very much for taking time to give feedback. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.
@PatilShirish
@PatilShirish 11 жыл бұрын
Thank you venkat It's really good and understandable for every one.
@Csharp-video-tutorialsBlogspot
@Csharp-video-tutorialsBlogspot 11 жыл бұрын
Hi, Editor and Display templates are discussed in Parts 44 to 46. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.
@umangpatwa8484
@umangpatwa8484 8 жыл бұрын
EditorModel method auto-loop by itself or what ? how it shows all data from city table without using for loop to show all city with check boxes
@avinashg2008
@avinashg2008 6 жыл бұрын
good one...new way to implement using editor template..
@waiyanhein7205
@waiyanhein7205 10 жыл бұрын
What is wrong ?? Can u explain ?? Cos u generate only once the checkbox in both templates . But it showing all . Why ? EditorModel method auto-loop by itself or what ??
@RuhiGoktas
@RuhiGoktas 11 жыл бұрын
A very vital point you have to pay attention here is while you create tblCity DO NOT check allow nulls if you do that, your code will give you an error saying "Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)"
@sprakash1094
@sprakash1094 10 жыл бұрын
IN edmx part set the IsSelected Property to false . its working for me. Thanks
@PoomulusRobsBanksOnTV
@PoomulusRobsBanksOnTV 7 жыл бұрын
Make sure your IsSelected column in your database is of type bit and that the IsSelected property inside your DataModel is a boolean. It should solve the issue since a bit returns true on 1 and false on 0
@syednoohu8692
@syednoohu8692 11 жыл бұрын
I'm confused between generating the RadioButtonFor and CheckBoxList We are using ForEach loop to generate RadioButton for each Department but in the case of CheckboxList not, What makes the difference? this is because of EditorforModel Templates ? sorry for asking naïve Question Thanks
@intersowa1970
@intersowa1970 10 жыл бұрын
Yes. "EditorForModel returns an HTML input element for each property in the model, using the template name, HTML field name, and additional view data." I did not know that before watching this episode tutorial. Fantastic.
@mellis321
@mellis321 11 жыл бұрын
Hi, thank you so much for this video! But, when I created the 'City' view page and ran the app, I noticed in the browser's source view that the entire HTML code itself was duplicated more than once. I decided to recreate the 'City' view as a partial view and now the source code is structured properly. I just wanted to bring this to your attention! :)
@jayrcodes91
@jayrcodes91 9 жыл бұрын
Hi Venkat, thank you for the effort for sharing your knowledge to everyone who wants to become a web developer. I just want to ask regarding to the IEnumerable parameter in HttpPost Index action method, when I try to write the IF statement, it doesn't provide a Count() function just like what you have used in the video. By the way I am using MVC4,VS2013 & EF 5.
@khoao1636
@khoao1636 8 жыл бұрын
have you tried to put System.LINQ namespace into you controller?
@jayrcodes91
@jayrcodes91 8 жыл бұрын
Right, I forgot to include the System.Linq namespace. I didn't notice that It was removed during I saved the project. My VS automatically get rid the unused namespaces during save. Hehe, this is already okay. Thanks for the response anyway.
@yamarat1
@yamarat1 11 жыл бұрын
Thank you for these videos! You are saying that editor templates are discussed in a later tutorial. which tutorial number are editor templates discussed? I could not find it. Thank you in advance!
@pathivadachakravarthy3905
@pathivadachakravarthy3905 8 жыл бұрын
Hi Venkat, While calling @Html.EditorForModel() showing error like External component has thrown an exception. Please suggest what i need to do?
@sca11ywagg
@sca11ywagg 5 жыл бұрын
IsSelected? Is it good that your database contains the UI's state? Food for thought.
@rajareddyanapareddy6490
@rajareddyanapareddy6490 2 жыл бұрын
Sir, what is the need of sending db.cities passing to index View.. where we are using those.
@maximss
@maximss 8 жыл бұрын
I also did this after sb.Remove() - sb.Replace(",", " and ", sb.ToString().LastIndexOf(","), 1); Looks more fluent.
@kondareddy6162
@kondareddy6162 6 жыл бұрын
checkbox is treated as id,name as isselected when you post the form data you select which checkbox what relation between checkbox and hidden element how to know us correct value come from hidden if you select one checkbox beside value as london just consider how to read correct value modelbinder london in hidden input element i hope u understood my point
@sachingreat222
@sachingreat222 7 жыл бұрын
In this video very important thing is described that is how to get selected checkbox from database
@saradatandukar7253
@saradatandukar7253 6 жыл бұрын
@Html.CheckBoxFor(x=> x.IsSelected) "Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type" there is problem in selected
@gurunathrao2985
@gurunathrao2985 6 жыл бұрын
For this error did the following and it worked for me: 1.IN SQL , drop the IsSelected Column using (alter table Cities1 drop column IsSelected). 2.Then create the same column again except use (alter table Cities1 add IsSelected BIT NOT NULL default 0) 3.Go to your model.edmx file>>right click on the entity>>Update you model from database. 4.Save your work and build and the error goes as IsSelected is not nullable anymore
@venkyL-cz2hl
@venkyL-cz2hl 5 ай бұрын
@@gurunathrao2985 thank u very much for ur solution it's very helpful for others and me to solve this error.
@gurunathrao2985
@gurunathrao2985 6 жыл бұрын
[HttpPost] [ActionName("Index")] public string Index_Post(IEnumerable ResCities) { if(ResCities.Count(x => x.IsSelected == true) == 0) { return "Are you kidding...select something!!"; } else { StringBuilder sb = new StringBuilder(); sb.Append("You Selected -"); foreach(Cities temp in ResCities) { if(temp.IsSelected == true) { sb.Append(temp.Name + ","); } } sb.Remove(sb.ToString().LastIndexOf(","), 1); return sb.ToString(); } } I think this slightly modified version of the [HttpPost] method is gonna help. Also please try making the IsSelected field in the table as non-nullable
@donwiesmann5186
@donwiesmann5186 9 жыл бұрын
How would this example work if the checkbox list was a List in a larger class of objects? You wouldn't set the view a IEnumerable as only one object is the List. If I do a @foreach loop, I can display the check boxes but I can't figure out how to retain the List of items that were checked for the HttpPost.Thanks you in advance!
@abubakarizhar5608
@abubakarizhar5608 10 ай бұрын
Dear can we display check boxes in foreach loop.
@nanda3281
@nanda3281 11 жыл бұрын
sir when I put it view of home folder it does not work instead it shows 12345 number with button but I put it shared folder it works correctly.
@sunilparmar6434
@sunilparmar6434 4 жыл бұрын
use '@' before (Html.BeginForm()) like this: (@Html.BeginForm())
@shahzaibriaz6552
@shahzaibriaz6552 6 жыл бұрын
I'm getting error Templates can be used only with field access ,property access , single dimension array index, or single parameter custom indexer expression?
@yeasinahmed4819
@yeasinahmed4819 6 жыл бұрын
Please set that your IsSelected column to not null in your database table. Then update your model from edmx and finally remove null-ability from IsSelected Property from city class. Here is my city class: public partial class City { public int Id { get; set; } public string Name { get; set; } public bool IsSelected { get; set; } }
@yamikadesai3931
@yamikadesai3931 4 жыл бұрын
@@yeasinahmed4819 This is not working...
@saminashaikh5564
@saminashaikh5564 5 жыл бұрын
This is useful,But i want Mvc with jquiry code for selected multiple Checkbox and store in database how it is can u help
@mahidharguggilam2435
@mahidharguggilam2435 6 жыл бұрын
you can trim it with comma
@Junaid7671
@Junaid7671 7 жыл бұрын
veenkat could you please make a video for how to create reusable checkbox list in mvc .
@HemantKumar-yk2jk
@HemantKumar-yk2jk 11 жыл бұрын
when I put it view of home folder it does not work instead it shows 12345 number with button
@dineshengg408
@dineshengg408 11 жыл бұрын
Hi Venkat, me too stuck at the same point. Not sure how to get rid of this. Could you help us in fixing this error. Code residing in the Views is same. But still not getting the expected o/p.
@developernader
@developernader 11 жыл бұрын
- check the folder name EditorTemplates - In database change IsSelected Not Allow Null - Update your DataMobile object -In City.cs public bool IsSelected { get; set; }
@Piotruch88
@Piotruch88 10 жыл бұрын
I have the same problem :( @developernader I checked and it still does not work
@badrulhussain5545
@badrulhussain5545 10 жыл бұрын
developernader hi i followed this instruction but i still get the output as 12345?
@shyamal890
@shyamal890 9 жыл бұрын
Hemant Kumar I too am getting the same error. kudvenkat can you please help
@adishjain3073
@adishjain3073 8 жыл бұрын
I am getting error in this line of code "@Html.CheckBoxFor(x=>x.IsSelected)". Getting red square line under “x.IsSelected”. i.e "Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type" How resolve this?
@TheRohannanu
@TheRohannanu 8 жыл бұрын
Convert all null values to 0 in tblCity for column isSelected
@umangpatwa8484
@umangpatwa8484 8 жыл бұрын
set IsSelected property as NOT NULL in database
@dineshkumarachari8757
@dineshkumarachari8757 7 жыл бұрын
public Nullable IsSelected { get; set; } change the this formate code public Boolean IsSelected { get; set; } is correct..i think this is help full for you..
@PoomulusRobsBanksOnTV
@PoomulusRobsBanksOnTV 7 жыл бұрын
Make sure your IsSelected column in your database is of type bit and that the IsSelected property inside your DataModel is of boolean. It should solve the issue since a bit returns true on 1 and false on 0 so it can be converted to a boolean type. Having the column set to not null would also be a good idea
@shahzaibriaz6552
@shahzaibriaz6552 6 жыл бұрын
@Html.CheckBoxFor(m=> m.IsSelected ?? false)
@killers512
@killers512 10 жыл бұрын
It would be simplier to to use string.Join method
@meghamisra5622
@meghamisra5622 6 жыл бұрын
Can i put them in to dropdown?
@jenithjana3652
@jenithjana3652 4 жыл бұрын
Hai all this comment is for those who get 1234 in the browser instead of checkboxes. Be careful with the spelling while naming the folder EditorTemplates. Hope this helps!!!
@tapankumarbaral1244
@tapankumarbaral1244 3 жыл бұрын
thnx
@venkyL-cz2hl
@venkyL-cz2hl 5 ай бұрын
thank u very much for this solution
@kavithavishwanathen6778
@kavithavishwanathen6778 8 жыл бұрын
Error CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
@vikaspandey72
@vikaspandey72 6 жыл бұрын
achu r in httppost action, return null
@muhammadshoaibkhan8428
@muhammadshoaibkhan8428 9 жыл бұрын
isselected property give me a problem.Its generate error of can't explacity convert type bool to bool. what to do
@ABHISHEKPATHAK49
@ABHISHEKPATHAK49 9 жыл бұрын
+Shoaib Khan Friend because IsSelected is nullable type in database, make it non nullable. or @Html.CheckBoxFor(m => m.IsSelected ?? false) try this. Please reply if it works
@braScene
@braScene 8 жыл бұрын
Just change the IsSelected property type from "Nullable" to just "bool".
@ABHISHEKPATHAK49
@ABHISHEKPATHAK49 8 жыл бұрын
thanks a lot dear.
@shahzaibriaz6552
@shahzaibriaz6552 6 жыл бұрын
@Html.CheckBoxFor(m=> m.IsSelected ?? false)
@shahzaibriaz6552
@shahzaibriaz6552 6 жыл бұрын
That's Bad thing because our entity is Auto-Generated
@krishnagoriparthi4570
@krishnagoriparthi4570 4 жыл бұрын
I didn't get any message when i was clicked the check box and submit button plz help me kudvenkat sir
@cabritoloko1
@cabritoloko1 5 жыл бұрын
Don't you need a for our a while to display all the check boxes? without it it should only display 1 checkbox
@dineshkumarachari8757
@dineshkumarachari8757 7 жыл бұрын
Hi Sir, From where the Cities Property comes in this code [[return View(db.Cities)] ] you not mansion the Cities Properties code .Please Send the code..
@rrgaming7244
@rrgaming7244 5 жыл бұрын
if you create table city and add into entity model then it comes auto
@kavithavishwanathen6778
@kavithavishwanathen6778 8 жыл бұрын
CS0019 Operator '==' cannot be applied to operands of type 'int' and 'bool'.....help people help
@kavithavishwanathen6778
@kavithavishwanathen6778 8 жыл бұрын
CS0029 Cannot implicitly convert type 'int' to 'bool' ..m getting this error
@shahzaibriaz6552
@shahzaibriaz6552 6 жыл бұрын
@Html.CheckBoxFor(m=> m.IsSelected ?? false)
@arun9385
@arun9385 9 жыл бұрын
Whats the use of City.cshtml what it is rendering and what is Index.cshtml for i am confused Sob Sob
@arun9385
@arun9385 9 жыл бұрын
Arun Nagar Why you put City in Editor template i put it in home folder O/P is 12345
@arun9385
@arun9385 9 жыл бұрын
Arun Nagar You should Reply people are crying here
@tictac18g-j1c9q
@tictac18g-j1c9q 9 жыл бұрын
Mahender Kumar gave answer as below. "EditorTemplates"
@sachingreat222
@sachingreat222 7 жыл бұрын
from where we will get code
@sridevimahapatra3046
@sridevimahapatra3046 8 жыл бұрын
Got 12345 output instead of Checkboxes.
@gh0stdagger
@gh0stdagger 8 жыл бұрын
Make sure your folder name is "EditorTemplates"
@sridevimahapatra3046
@sridevimahapatra3046 8 жыл бұрын
I named it right but still didn't get the Out put. Actually the task was to load Check boxes dynamically with data from DB,(like Cuisines-Indian,Chines,Italian).Kindly let me know if you have any idea about how to do it. Thanks in advance.
@rasmiranjanmishra8271
@rasmiranjanmishra8271 8 жыл бұрын
Hi Sridevi The issue is your view is unable to get data from EditorForModel control . Make sure your view and EditorTemplates folder are in same folder
@sridevimahapatra3046
@sridevimahapatra3046 8 жыл бұрын
Hi Rashmi Ranjan Mishra. Thank you.
@rrgaming7244
@rrgaming7244 5 жыл бұрын
Thanks Dear​@@rasmiranjanmishra8271
@HemantKumar-yk2jk
@HemantKumar-yk2jk 11 жыл бұрын
Can u please explain why it is happened ..
@mahender18
@mahender18 11 жыл бұрын
Please check the folder name in view->Home->EditorTemplates,it helped me
@attiqafridi9094
@attiqafridi9094 9 жыл бұрын
+Mahender Kumar a bundle of thanks, you made it easy for me...
@mohanbehara6262
@mohanbehara6262 7 жыл бұрын
hey im geting 1234 insted of checkboxes i kept "EditorTemplates"
@sunilparmar6434
@sunilparmar6434 4 жыл бұрын
use '@' before (Html.BeginForm()) like this: (@Html.BeginForm())
@DeathHordes
@DeathHordes 10 жыл бұрын
dude...
@shahzaibriaz6552
@shahzaibriaz6552 6 жыл бұрын
Worst Video totally confusing
@desitrump
@desitrump 5 жыл бұрын
@@TheVibhu100 He is right, its totally confusing video.
@codingislife6387
@codingislife6387 Жыл бұрын
My @Html.EditorForModel() not Gettng EditorTemplates don't know whats wrong . I am doing all steps correctly
Part 39   ListBox in asp net mvc
14:23
kudvenkat
Рет қаралды 129 М.
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
Part 45   Customize display and edit templates in asp net mvc
14:07
Gemini 2.0 Pro
17:41
Prompt Engineering
Рет қаралды 36 М.
Hamas releases three Israeli captives
11:18
Al Jazeera English
Рет қаралды 311 М.
Part 25  Insert update delete in mvc using entity framework
10:27