I have a bunch of come delimited items on my db table column.
Example
Peter, Greg, Paul, Mary,
My question is, how do I display these items as checked Items on my view?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
R J192 reputationPosted Aug 26, 2014, 1:29 PM EDT
you can simply use Split(',');
see below is example.
var query = from o in db.Students
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 21, 2014, 10:10 AM EDT
http://stick2basic.wordpress.com/2013/03/12/checkboxlist-in-asp-net-mvc4/
http://forums.asp.net/t/1943984.aspx?How+to+use+checkbox+and+submit+the+check+values+
ceci bela49 reputationPosted Aug 21, 2014, 12:17 PM EDT
Db Example.
---------------------------------------------
| Name | Last Name | PlacesVisited |
---------------------------------------------
| Jonh | Doe | NY, NJ, CA, RI, |
----------------------------------------------
| Mary | Lou | LA, CO, OR, NY |
---------------------------------------------
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 21, 2014, 1:19 PM EDT
don't know this works or not
ceci bela49 reputationPosted Aug 22, 2014, 12:19 PM EDT
ceci bela49 reputationPosted Aug 26, 2014, 1:14 PM EDT
LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method, and this method cannot be translated into a store expression.
When I tried this on my homecontroller
Any ideas how to use the Split() correctly with Linq?
Thanks
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 26, 2014, 1:35 PM EDT
List
as Rj said u can do in this way
var resu1 = db.Students.where(o => o.StudentID == "200312").select ( o => o.PlacesVisited.Split(',')).ToList();
ceci bela49 reputationPosted Aug 27, 2014, 10:36 AM EDT
I tried both suggestions (but without creating a new list) and got the same error.
LINQ to Entities does not recognize the method 'System.String[] Split(Char[])'
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 27, 2014, 11:02 AM EDT
ceci bela49 reputationPosted Aug 27, 2014, 11:40 AM EDT
Screenshot.
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 27, 2014, 11:52 AM EDT
List
ceci bela49 reputationPosted Aug 27, 2014, 12:52 PM EDT
See image.
Now how can I get this into checkboxes on my View without the @Html.CheckBoxFor helper function?
I am thinking something like this:
@PlacesVisited
I might be wrong but I don't think the @Html.CheckBoxFor() is friendly in my case.
Thanks a bunch.
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 27, 2014, 1:06 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 27, 2014, 1:07 PM EDT
ceci bela49 reputationPosted Aug 28, 2014, 12:34 PM EDT
Controller
// Response.Write("Debug:"+ViewBag.CheckedItems) <= this works.
View
NOTE: if I do a Response.Write(ViewBag.CheckedItems) on the controller I can see all the items.
Thanks guys I really appreciate it.
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 28, 2014, 12:41 PM EDT
I mean what your getting if you do like this item. after item . what intelisense giving ?
ceci bela49 reputationPosted Aug 28, 2014, 12:55 PM EDT
But the controller shows there is data as shown here.
https://www.dropbox.com/s/swkvozcga0ofnwm/debug.png?dl=0
is it the way I am saving the data in ViewBag or is it the problem when I access the ViewBag?
ceci bela49 reputationPosted Aug 28, 2014, 1:10 PM EDT
I got the error below.
The type or namespace name 'CheckListBoxItem' could not be found (are you missing a using directive or an assembly reference?)
When I tried return View(ViewBag.CheckedItems)
I got this error:
Exception Details: System.InvalidOperationException: The view 'NY NJ CA ND ' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/home/NY NJ CA ND .aspx
~/Views/home/NY NJ CA ND .ascx
~/Views/Shared/NY NJ CA ND .aspx
~/Views/Shared/NY NJ CA ND .ascx
~/Views/home/NY NJ CA ND .cshtml
~/Views/home/NY NJ CA ND .vbhtml
~/Views/Shared/NY NJ CA ND .cshtml
~/Views/Shared/NY NJ CA ND .vbhtml
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 28, 2014, 1:14 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 28, 2014, 1:31 PM EDT
1)
return View(myString.AsEnumerable)
2)
//controller
List
return View(myString );
//view
@model List
// and property Model is type of List
@foreach(var item in Model)
{
}
ceci bela49 reputationPosted Aug 29, 2014, 9:34 AM EDT
Thank you both Ramesh & RJ!
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 29, 2014, 9:39 AM EDT