0

I am retrieving a string from a MS Sql table, it could be just one name or delimited names: For example Tom or Speaker 1, Speaker 2.
I convert the string to list in the controller using:

                    Event cevent = db.Events.Find(id); 
                    string speakers = cevent.Speakers;     
                    ViewBag.speakers = "";
                    if (!String.IsNullOrWhiteSpace(speakers) && 
                    speakers.Contains(","))
                    {
                        ViewBag.speakers = speakers.Split(',').ToList();
                    }
                    else
                    {
                        ViewBag.speakers = speakers;
                    }
                    return View(cevent);

In the view I use the following to display the list:

         <ul>
            @foreach (var item in ViewBag.speakers)
            {
                <li> @item </li>
            }
        </ul>

Works great with the list, I get:

• Speaker 1
• Speaker 2

However, if the ViewBag has just one item, I get:

• T 
• o 
• m 
2
  • var list=ViewBag.speakers as List<string>(); and check if the list.count >0 use foreach else bind the normal item Commented Dec 28, 2016 at 3:24
  • or you can modify string to list<string>, so no need of further process in view. if speakers as a string then, List<string> obj1 = new List<string>(); obj1.Add(speakers); ViewBag.speakers = obj1 ; Commented Dec 28, 2016 at 3:31

1 Answer 1

1

You might consider always passing an enumerable speaker object to the view regardless of whether or not there are one or more than one speaker.

In your view, you are enumerating over the speakers object, which, in the case of the "Tom", is a list of chars. You might try something like this, instead:

In the controller:

ViewBag.speakers = new List<string>();
string speakers = cevent.Speakers;
var listOfSpeakers = speakers.Split(',').ToList();

foreach (var speaker in listOfSpeakers)
{
    ViewBag.speakers.Add(speaker)
}

If you need to format the HTML output differently depending on whether more than one speaker is passed to the view, you can use if(ViewBag.speakers.Count > 1) on the speaker list in a conditional block and handle the output differently in that case.

Sign up to request clarification or add additional context in comments.

5 Comments

I get error 'string' does not contain a definition for 'Count'
@hncl Yes, you can only call count on an enumerable object. You may want to always pass a list to the view, even if it only contains one element. I have edited to suggest changes to controller.
I should clarify, .Count() is a LINQ extension method, and works on IEnumerable types, but .Count is a property and part of the List object itself.
thank you. I got this error in the controller: System.Collections.Generic.List<string>' does not contain a definition for 'add'
.Add() with capital A.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.