Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6320279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:56:35+00:00 2026-05-24T15:56:35+00:00

I have the following code: string init = yes; string html = ; foreach

  • 0

I have the following code:

        string init = "yes";
        string html = "";
        foreach (var item in v.Details) {
               if (item.Substring(0, 1) != " ")
               {
                   if (init != "yes")
                   {
                       html += "</ul>";

                   }
                   html += "<p>" + item + "</p><ul>";
               }
               else
               {
                   html += "<li>" + item.Substring(1) + "</li>";
               }
           }

The code is in my MVC controller and it creates a string called html. The thing is I don’t think it should be in the controller. I tried to put this into the view and ended up with a huge mess that doesn’t seem to work. Seems I am not very good at coding C within a razor view. I just saw a lot of syntax type errors and confusion between what’s C and what’s HTML.

Can anyone suggest how I could make this code work within a view. Here’s what I used to have:

        <ul>
        @foreach (var item in Model.Details)
        {
            <li>@item</li>
        } 
        </ul>

This worked but as you can see I now need more processing. Would it be better to take this out of the view and if so how could I do this. I’m really hoping for a view solution but I am confused about where the put the @’s and where to put the brackets.

Any experts at coding C inside or Razor out there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:56:37+00:00Added an answer on May 24, 2026 at 3:56 pm

    The code is in my MVC controller and it creates a string called html.
    The thing is I don’t think it should be in the controller

    You are correct. It shouldn’t be in the view neither due to the absolute mess it would create. I think this code is better suited in a custom HTML helper:

    public static class HtmlExtensions
    {
        public static IHtmlString FormatDetails(this HtmlHelper htmlHelper, IEnumerable<string> details)
        {
            var init = "yes";
            var html = new StringBuilder();
            foreach (var item in details)
            {
                if (item.Substring(0, 1) != " ")
                {
                    if (init != "yes")
                    {
                        html.Append("</ul>");
                    }
                    html.AppendFormat("<p>{0}</p><ul>", htmlHelper.Encode(item));
                }
                else
                {
                    html.AppendFormat("<li>{0}</li>", htmlHelper.Encode(item.Substring(1)));
                }
            }
            return MvcHtmlString.Create(html.ToString());
        }
    }
    

    which you would invoke in your view:

    @Html.FormatDetails(Model.Details)
    

    Remark: there seems to be something wrong with the init variable. You are setting its value to yes initially but you never modify it later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently I have the following code: String select = qry.substring(select .length(),qry2.indexOf( from )); String[]
I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);
I have the following code: // Obtain the string names of all the elements
I have a C# application that includes the following code: string file = relativePath.txt;
I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie
Okay so I have the following Code which appends a string to another in
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
I have the following code, which splits up a Vector into a string vector
I have the following code: public static T ParameterFetchValue<T>(string parameterKey) { Parameter result =
I have the following piece of code pattern: void M1(string s, string v) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.